There are many ways to get the time in JavaScript. Generally, you can refer to the moment.js library to get the time, or you can get the time by writing your own code.
Getting the time in moment.js involves downloading and quoting moment.js. The details can be viewed on the official website (http://momentjs.cn/timezone/). Let’s talk about how to get the time after moment.js is configured. .
For obtaining the current time:
moment().format('L'); // 2018-03-01 moment().format('l'); // 2018-03-01 moment().format('LL'); // March 1, 2018 moment().format('ll'); // March 1, 2018 moment().format('LLL'); // March 1, 2018 at 6:02pm moment().format('lll'); // March 1, 2018 at 6:02 PM moment().format('LLLL'); // Thursday, March 1, 2018 at 6:02 PM moment().format('llll'); // Thu Mar 1, 2018 6:02pm
Also date formatting
moment().format('MMMM Do YYYY, h:mm:ss a'); // March 1st 2018, 6:08:27 PM moment().format('dddd'); // Thursday moment().format("MMM Do YY"); // 3月 1日 18 moment().format('YYYY [escaped] YYYY'); // 2018 escaped 2018 moment().format(); // 2018-03-01T18:08:27+08:00
The above functions need to be implemented after referencing moment.js.
There is also a way to write your own code to complete
function time(){ var myDate = new Date(); var year=myDate.getFullYear(); var month=myDate.getMonth(); var newMonth = (month+1)>9?(month+1):"0"+(month+1); // 03月 var day=myDate.getDate(); var newday = day>9?day:"0"+day; // 09日 var hours=myDate.getHours(); var newhours = hours>9?hours:"0"+hours; // 06时 var minutes=myDate.getMinutes(); var newminutes = minutes>9?minutes:"0"+minutes; // 06分 var seconds=myDate.getSeconds(); var newseconds= seconds>9?seconds:"0"+seconds; // 03秒 var now_time= year+"年"+ newMonth+"月"+newday+"日 "+ newhours+":"+newminutes+":"+newseconds; return now_time } var now_time=time(); console.log(now_time) //Output the current time such as March 01, 2018 18:13:49
The above code can be changed according to the time format we need
If you do not need to add 0 in front of the month or day number, then the var newMonth = (month+1)>9?(month+1):"0"+(month+1); in the code segment is canceled, and the corresponding code is slightly modified, namely Can.
Another point to remember is that the month is 0-11, so +1 is the current month.
Another way is to get relative time
moment("20111031", "YYYYMMDD").fromNow(); // 6 年前 The upstream code is to get 20111031 relative to the current time because 20111031 to 20180331 is 6 years and 6 months, and today is 20180302. The extra month is less than 6 months, so it is 6 years ago. moment("20120620", "YYYYMMDD").fromNow(); // 6 年前 The upstream code is to get 20120620 relative to the current time because 20120620 to now 20180302 is 5 years and 7 months more than 7 months more than half a year, so it is rounded by 1 year, so it is 6 years ago moment().startOf('day').fromNow(); // 11 hours ago The above code is to get how many hours have passed since the previous day. For example, it is 10.34 minutes and more than 30 minutes, and the round is 1 hour, so the previous day is 11 hours ago. moment().startOf('day').fromNow(); // 10 hours ago The above code is to get how many hours have passed from the previous day to the present. For example, it is 10.29 minutes and less than 30 minutes, and 29 minutes are rounded off, so it is 10 hours ago from the previous day. moment().endOf('day').fromNow(); // within 13 hours The upstream code is to get how many hours the next day is from now, or how long it will be until the next day. For example, it is now 10.34 minutes greater than 30 minutes, and the round is 1 hour, so the round is 11 o'clock, so there are 13 hours to the next day moment().endOf('day').fromNow(); // within 14 hours The upstream code is to get how many hours the next day is from now, or how long it will be until the next day. For example, it is now 10.29 minutes less than 30 minutes, rounding off 29 minutes, so the time is now 10 o'clock, so there are still 14 hours to the next day moment().startOf('hour').fromNow(); // 34 minutes ago The above code is how long it has been since the last hour is obtained, and it is also how many minutes ago was the hour. For example, it is 10.34 now, so the last hour 10 is 34 minutes ago.
The above is my personal understanding, please let me know if I am wrong.