let now = newDate(); console.log(now); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 (India Standard Time) console.log(typeof now); // object console.log(now.getMonth()); // 4
let strnow = Date(); console.log(strnow); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 (India Standard Time) console.log(typeof strnow); //string
console.log(strnow.getMonth()); //Uncaught TypeError: strnow.getMonth is not a function
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getTimezoneOffset()); // -330
getDate(): 返回一个表示日期的整数(1 到 31)。
1 2 3
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getDate()); // 14
getDay(): 返回本地时间的星期几(0 到 6),0 代表星期日,不可更改
1 2 3
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getDay());
getMonth(): 返回表示本地时间月份的整数,月份从0到11。
1 2 3
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getMonth()); // 4
getFullYear():返回本地日期的年份,年份用4位数字表示。
1 2 3
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getFullYear()); // 2021
getHours():返回当地时间的当前小时。
1 2 3
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getHours()); // 20
getMinutes():返回当地时间的当前分钟数。
1 2 3
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getMinutes()); // 29
getSeconds(): 返回本地时间的当前秒数。
1 2 3 4 5 6 7
let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getSeconds()); // 44 9. getMilliseconds():返回本地时间的毫秒数。 let now = newDate(); // Current Time: Fri May 14 2021 20:29:55 GMT+0530 console.log(now.getMilliseconds()); // 251
//Current Time: Fri May 14 2021 21:28:29 GMT+0530 (India Standard Time) now = newDate(); now.setHours(23); console.log(now); // Fri May 14 2021 23:31:59 GMT+0530 (India Standard Time)
setMinutes():设置分钟。您可以指定秒和毫秒作为可选参数。
1 2 3 4 5 6
setMinutes(min, [sec], [ms]) //Current Time: Fri May 14 2021 21:28:29 GMT+0530 (India Standard Time) now = newDate(); now.setMinutes(00); console.log(now); // Fri May 14 2021 21:00:58 GMT+0530 (India Standard Time)
setSeconds():设置秒数。您还可以指定毫秒作为可选参数。
1 2 3 4 5
// Current Time: Fri May 14 2021 21:28:29 GMT+0530 (India Standard Time) now = newDate(); now.setSeconds(00); console.log(now); // Fri May 14 2021 21:33:00 GMT+0530 (India Standard Time)
setMilliseconds(): 设置毫秒。
1 2 3 4 5
// Current Time: Fri May 14 2021 21:28:29 GMT+0530 (India Standard Time) now = newDate(); now.setMilliseconds(00); console.log(now); // Fri May 14 2021 21:34:32 GMT+0530 (India Standard Time)
您可以将字符串转换为 Date 对象,Date 对象的构造函数采用不同格式的字符串。下面给出了一些例子。
const date1 = newDate("Fri, May 14 2021 21:00:00"); console.log(date1); //Fri May 14 2021 21:00:00 GMT+0530 (India Standard Time)
const date2 = newDate("Fri, May 14 2021 21:00:00 UTC"); console.log(date2); // Sat May 15 2021 02:30:00 GMT+0530 (India Standard Time) // the output is in Indian standard time not in UTC, // i.e. 5:30 is added to 21:00 // so we get 02:30
const date3 = newDate("14 May 2021 21:00:00 UTC+05:30"); console.log(date3); // Fri May 14 2021 21:00:00 GMT+0530 (India Standard Time)
const date4 = newDate(2021, 4, 14, 21, 00, 0); console.log(date4); // Fri May 14 2021 21:00:00 GMT+0530 (India Standard Time) // Rememnber month starts from zero
const date5 = newDate("2021-05-14T21:00:00Z"); console.log(date5) // Sat May 15 2021 02:30:00 GMT+0530 (India Standard Time)
Date 对象的输出是 object,我们可以将其转换为字符串格式,Date 对象有内置的方法。
toString() :返回 Date 对象的字符串表示形式。
toLocalString() :以本地格式返回 Date 对象的字符串表示形式。
toTimeString():返回 Date 对象的时间部分。
toLocalTimeString() :以本地格式返回 Date 对象的时间部分。
toDateString() :返回 Date 对象的日期部分。
toLocalDateString() :以本地格式返回 Date 对象的日期部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
console.log(typeof now.toString(), now.toString()); // string Fri May 14 2021 21:48:19 GMT+0530 (India Standard Time)
// Print Day functionprintWeekday(){ let names = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; console.log(`Today is ${names[newDate().getDay()]}`); } printWeekday(); //Today is Tuesday
4. 当前月份
1 2 3 4 5 6 7
// Print Month functionprintMonthName(){ let names = ['January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October',' November', 'December']; console.log(`The month is ${names[newDate().getMonth()]}`); } printMonthName(); //The month is May
5. 每月第一天和最后一天
1 2 3 4 5 6 7 8 9
// Get first and last day of month functionfirstLastDay(year,month){ let dateFirst = newDate(year,month-1,1); let dateLast = newDate(year,month,0); console.log(dateFirst.toLocaleDateString(), dateLast.toLocaleDateString()); } firstLastDay(2021, 1); // 1/1/2021 1/31/2021 firstLastDay(2021, 2); // 2/1/2021 2/28/2021 firstLastDay(2021, 5); // 5/1/2021 5/31/2021
6. 一个月的天数
1 2 3 4 5 6 7 8 9
// Print Number of days in a month // Month starts from 0, so (year,month, 0) gives last day of month-1 functiongetDaysInMonth(year,month){ let days = newDate(year, month,0).getDate(); console.log(`Number of days in ${month}/${year}: ${days}`); } getDaysInMonth(2021, 1); // Number of days in 1/2021: 31 getDaysInMonth(2021, 2); // Number of days in 2/2021: 28 getDaysInMonth(2021, 5); // Number of days in 5/2021: 31
7.以AM/PM格式打印时间
1 2 3 4 5 6 7 8 9 10 11 12
// Find AM or PM time in hh:mm:ss format functionamOrpm(t){ let time = t.split(" ")[0].split(':'); if(parseInt(time[0]) >= 12){ parseInt(time[0]) === 12 ? 12 : parseInt(time[0] % 12); time.push("PM") }else{ time.push("AM") } console.log(`${time[0]}:${time[1]}:${time[2]}${time[3]}`); } amOrpm(newDate().toTimeString()); // 17:31:07 PM
8. 计算两个日期之间的天数
1 2 3 4 5 6 7 8
// Calculate number of days between two dates functionnoDates(first, last){ let difference = Math.ceil((last - first) / (1000*60*60*24)); console.log(`The number of days between ${first.toLocaleDateString()} and ${last.toLocaleDateString()} is ${Math.abs(difference)} days`); }
noDates(newDate('2018-1-1'), newDate('2021-05-15')) //The number of days between 1/1/2018 and 5/15/2021 is 1231 days
9. 计算两个日期之间的月数
1 2 3 4 5 6 7 8 9 10 11
// Calculate number of months between two dates functionnumMonths(first, last){ let yearToMonths = [last.getFullYear() - first.getFullYear()] * 12; let months = [yearToMonths + (last.getMonth() + 1)] - (first.getMonth() + 1); console.log(`Number of months between ${first.toLocaleDateString} and ${last.toLocaleDateString} is ${months}`); }
numMonths(newDate('2018-05-21'), newDate('2020-05-21')); // Number of months between 5/21/2018 and 5/21/2021 is 36