下一月的第0天就是當前月的最後一天。javascript
function daysInMonth(year, month) {
let date = new Date(year, month + 1, 0);
return date.getDate();
}
// 注意在JS裏月份是從0開始算的。下面算的是2017年3月份有多少天
console.log(daysInMonth(2017, 2)); // 31
// 2017年2月有多少天
console.log(daysInMonth(2017, 1)); // 28
// 2016年2月有多少天
console.log(daysInMonth(2016, 1)); // 29複製代碼
let now = new Date();
console.log(now.toISOString()); //2017-08-05T13:16:35.363Z
//中國是東八區,北京時間是(GMT+08:00)
console.log(now.getTimezoneOffset()); // -480
//將本地時間換算成格林威治時間/零時區
let GMTDate = new Date(now.getTime() + now.getTimezoneOffset() * 60 * 1000);
console.log(GMTDate.toISOString()); //2017-08-05T05:16:35.363Z
//將本地時間換算成東3區時間
let eastZone3Date = new Date(GMTDate.getTime() + 3 * 60 * 60 * 1000);
console.log(eastZone3Date.toISOString()); //2017-08-05T08:20:55.235Z複製代碼
JSON.stringify({
a: 4,
b: [3, 5, 'hello'],
}, (key, val) => {
if(typeof val === 'number') {
return val * 2;
}
return val;
}); //{"a":8,"b":[6,10,"hello"]}複製代碼
JSON.stringify({
a: 4,
b: {
a: 5,
d: 6
},
c: 8
}, ['a', 'b']); //{"a":4,"b":{"a":5}}複製代碼
JSON.stringify({
a: [3,4,5],
b: 'hello'
}, null, '|--\t');
/**結果: { |-- "a": [ |-- |-- 3, |-- |-- 4, |-- |-- 5 |-- ], |-- "b": "hello" } */複製代碼