Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
96
4
(换言之,如果把日设为
40
JavaScript
直接把日期延到下一个月)。
Date
构造
函数还接受在不同的计算机中解析结果不一致的字符串。
如果使用非数字字符串创建
Date
对象,得到的是一个“
Invalid Date
”(无效日期)
对象。这种情况可以使
isNaN()
法检查。
const
badDate = '12 bananas';
const
convertedDate =
new
Date(badDate);
if (Number.isNaN(convertedDate)) {
//
代码执行到这里,因为没有成功创建日期对象
}
else
{
//
如果是有效的
Date
实例,代码执行到这里
}
之所以可以这样检查,是因为
Date
对象的背后其实是数字,详见
4.4
节。
4.2.4
参考
4.6
节说明反向操作,即把
Date
对象转换成字符串。
4.3
在日期上增加天数
4.3.1
问题
你想计算在某个日期之前或之后一定天数的日期。
4.3.2
方案
使用
Date.getDate()
方法获取当前日期是一个月中的第几天,然后使用
Date.
setDate()
方法修改。
Date
象足够聪明,如有必要,会顺延到下个月或下一年。
const
today =
new
Date();
const
currentDay = today.getDate();
//
三周后的日期是什么?
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript

JavaScript

T. J. Crowder
JavaScript

JavaScript

David Flanagan
JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov

Publisher Resources

ISBN: 9787519869601