在項目中不可避免對日期進行操做,所以我對其進行總結,以便隨時翻閱。javascript
// "英文月名 日 年 時:分:秒"
const date = new Date("May 5 19 05:15:30");
console.log(date); // Sun May 05 2019 05:15:30 GMT+0800 (GMT+08:00)
// 年 月 日 時 分 秒
const date1 = new Date(2019, 4, 5, 5, 15, 30);
console.log(date1); // Sun May 05 2019 05:15:30 GMT+0800 (GMT+08:00)
// 月/日/年/時:分:秒
const date2 = new Date("5/5/2019 05:15:30");
console.log(date2); // Sun May 05 2019 05:15:30 GMT+0800 (GMT+08:00)
// 年-月-日T時:分:秒
const date3 = new Date("2019-05-05T05:15:30");
console.log(date3); // Sun May 05 2019 05:15:30 GMT+0800 (GMT+08:00)
複製代碼
const date = new Date();
// 完整日期
console.log(date.toString()); // Fri Sep 13 2019 10:41:11 GMT+0800 (GMT+08:00)
// 年-月-日 時:分:秒
console.log(date.toLocaleString()); // 2019-9-13 10:41:11
// 星期幾 月 日 年
console.log(date.toDateString()); // Fri Sep 13 2019
// 時:分:秒 時區
console.log(date.toTimeString()); // 10:41:11 GMT+0800 (GMT+08:00)
// 年-月-日
console.log(date.toLocaleDateString()); // 2019-9-13
// 時:分:秒
console.log(date.toLocaleTimeString()); // 10:41:11
複製代碼
getTime(); // 返回日期的毫秒數 例如:1568335062493 (13位)
const date = new Date("5/25/10");
console.log(date.getTime()); // 1274716800000
console.log(date.valueOf()); // 1274716800000
console.log(Date.parse(date)); // 1274716800000
console.log(Date.now()); // 獲取當前的日期的毫秒數
console.log(+new Date()); // 獲取當前的日期的毫秒數
console.log(Date.UTC(2010, 4, 25)); // 獲取UTC時間下的毫秒數 1274745600000
複製代碼
// 年
getFullYear(); // 取得四位數的年份 若是是三位數的年份,返回三位數
const date = new Date("5/25/100");
console.log(date.getFullYear()); // 100
const date1 = new Date("5/25/10");
console.log(date1.getFullYear()); // 2010
// 月
getMonth(); // 返回日期中的月份,0表示一月,11表示十二月
const date2 = new Date("5/25/100");
console.log(date2.getMonth()); // 4
const date3 = new Date("12/25/100");
console.log(date3.getMonth()); // 11
// 若是月份不是0-11整數,那麼返回NaN
const date4 = new Date("12/31/2019");
console.log(date4.getMonth()); // NaN
// 若是日期超過當月日期數,但不大於31,則月份+1,若是超過31則返回NaN
const date5 = new Date("6/32/2019");
console.log(date5.getMonth()); // NaN
// 日
getDate(); // 返回日期月份中的天數
const date6 = new Date("6/30/19");
console.log(date6.getDate()); // 30
// 若是月份不是0-11的整數,那麼返回NaN
const date7 = new Date("13/20/2019");
console.log(date7.getDate()); // NaN
// 若是日期超過當月日期數,但不大於31,則日期爲第二月超出的日期,若是超過31則返回NaN
const date8 = new Date("2/31/19");
console.log(date8.getDate()); // 3
const date9 = new Date("12/32/2019");
console.log(date9.getDate()); // NaN
// 星期
getDay(); // 返回日期中星期的星期幾(0表明星期日, 6表明星期六)
const date10 = new Date("2/15/19");
console.log(date10.getDay()); // 5
// 若是日期超過當月日期數,但不大於31,則日期爲第二月超出的星期值,若是超過31則返回NaN
const date11 = new Date("11/32/2019");
console.log(date11.getDay()); // NaN
複製代碼
// 時
getHours(); // 返回日期中的小時數(0到23)
const date = new Date(2019, 4, 5, 17, 55, 55);
console.log(date.getHours()); // 17
const date1 = new Date(2019, 4, 5, 25, 55, 55);
console.log(date1.getHours()); // 1
// 分
getMinutes();
const date2 = new Date(2019, 4, 5, 17, 55, 55);
console.log(date2.getMinutes()); // 55
const date3 = new Date(2019, 4, 5, 21, 65, 55);
console.log(date3.getMinutes()); // 5
// 秒
getSeconds(); // 返回日期中的秒數(0-59)
const date4 = new Date(2019, 4, 5, 17, 55, 55);
console.log(date4.getSeconds()); // 55
const date5 = new Date(2019, 4, 5, 21, 55, 60);
console.log(date5.getSeconds()); // 0
// 毫秒
getMilliseconds(); // 獲取日期中的毫秒數(0-999)
const date6 = new Date(2019, 4, 5, 17, 55, 55, 200);
console.log(date6.getMilliseconds()); // 200
const date7 = new Date(2019, 4, 5, 21, 55, 55, 1000);
console.log(date7.getMilliseconds()); // 0
複製代碼
獲取時間的語法前面都有
get
, 設置時間的語法與之對應的是前面都是set
java
// 例如設置這個日期 Sun May 05 2019 05:15:30 GMT+0800 (GMT+08:00)
const date = new Date();
date.setFullYear(2019);
date.setMonth(4);
date.setDate(5);
date.setHours(5);
date.setMinutes(15);
date.setSeconds(30);
console.log(date); // Sun May 05 2019 05:15:30 GMT+0800 (GMT+08:00)
複製代碼
/** * description: 把日期毫秒數獲得年月日 支持Number和String的毫秒數 * param {type} time-時間毫秒數 monthWord 0(英文月份縮寫) 1(英文月份全拼寫) 2(中文) * return: obj-{ year, month, day, week, hour, minute, second } */
function getTime(time, monthArg = 0) {
const abbrMonth = [
"JAN.",
"FEB.",
"Mar.",
"APR.",
"MAY.",
"JUN.",
"JUL.",
"AUG.",
"SEP.",
"OCT.",
"NOV.",
"DEC"
];
const enMonth = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"Auguest",
"September",
"October",
"November",
"December"
];
const zhMonth = [
"一月",
"二月",
"三月",
"四月",
"五月",
"六月",
"七月",
"八月",
"九月",
"十月",
"十一月",
"十二月"
];
const date = new Date(+time);
const year = date.getFullYear();
const monthNum = date.getMonth();
let month = "";
switch (monthArg) {
case 1:
month = enMonth[monthNum];
break;
case 2:
month = zhMonth[monthNum];
break;
default:
month = abbrMonth[monthNum];
break;
}
const day = date.getDate();
const week = date.getDay();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return { year, month, day, week, hour, minute, second };
}
const time = "1274716800000";
console.log(getTime(time)); // { year: 2010, month: 'MAY.', day: 25, week: 2, hour: 0, minute: 0, second: 0 }
複製代碼
全稱Coordinated Universal Time
即國際協調時間,在時刻上儘可能接近於格林尼(GMT)標準時間git
時區。全球 24 個時區,把以倫敦爲中心的世界地圖展開,UTC 和 GMT 的倫敦是 0 區,倫敦的右邊直到新西蘭的東部不遠的斐濟爲東區,而左邊到太平洋中間爲西區。通常用 5 位時區差來表示與 UTC 相差的小時數,東區爲正,西區爲負。如北京時區是東八區,領先 UTC 八個小時,記爲+0800;紐約的時區是西五區,比 UTC 落後五個小時,記爲 -0500。UTC + 時區差 = 本地時間github
const date = new Date();
console.log(date); // Fri Sep 13 2019 11:32:32 GMT+0800 (GMT+08:00)
console.log(date.getHours()); // 11
console.log(date.getUTCHours()); // 3
複製代碼
由於日常不涉及 UTC,所以就不展開了ide
繼續努力。ui