JS to get the current time (yesterday, today, tomorrow)

Time:2021-1-24

1. Time format

1 // yesterday's time
 2 var day1 = new Date();
 3 day1.setTime(day1.getTime()-24*60*60*1000);
 4 var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate();
 5 // today's time
 6 var day2 = new Date();
 7 day2.setTime(day2.getTime());
 8 var s2 = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate();
 9 // tomorrow's time
10 var day3 = new Date();
11 day3.setTime(day3.getTime()+24*60*60*1000);
12 var s3 = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate();
13 // splicing time
14 function show(){
15 var STR = "+ S1 +" to "+ S2;
16      return str;
17 }
18 // assign doubledate
19  $('#dateS').val(show());

2. The following is the specific time to get the method

1 function writeCurrentDate() {
 2         var now = new Date();
 3         var year =  now.getFullYear (); // get the year
 4         var month =  now.getMonth (); // get month
 5         var date =  now.getDate (); // get date
 6         var day =  now.getDay (); // get weeks
 7         var hour =  now.getHours (); // get hours
 8         var minu =  now.getMinutes (); // get minutes
 9         var sec =  now.getSeconds (); // get seconds
10        var MS =  now.getMilliseconds (); // get MS
11         var week;
12         month = month + 1;
13         if (month < 10) month = "0" + month;
14         if (date < 10) date = "0" + date;
15         if (hour < 10) hour = "0" + hour;
16         if (minu < 10) minu = "0" + minu;
17         if (sec < 10) sec = "0" + sec;
18         if (MS < 100)MS = "0" + MS;
19         var arr_ Week = new array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
20         week = arr_week[day];
21         var time = "";
22 time = year + month + month + date + day + "+ hour +": "+ min +": "+ sec +" + week;
23 // assign the current date to the current date input box (jQuery easyUI)
24         $("#currentDate").html(time);
25 // set the execution interval of the function to get the current date, and refresh it every 1000 ms.
26         var timer = setTimeout("writeCurrentDate()", 1000);
27       }

3. Updated on June 27, 2017
Today, a simple method has been found: you can directly operate on the date, hour, minute and second. If today is June 1, 2017, then yesterday is May 31, 2017

1 // yesterday's time 
2  var day1 = new Date();
3  day1.setDate(day1.getDate() - 1);
4  var s1 = day1.format("yyyy-MM-dd");
5 // the day before yesterday
6  var day2 = new Date();
7  day2.setDate(day2.getDate() - 2);
8  var s2 = day2.format("yyyy-MM-dd");

The format function is an extension function.

1 /**
 2 * the extension of date to convert date to string in specified format
 3 * month (m), day (d), hour (H), minute (m), second (s), quarter (q) can use 1-2 placeholders,
 4 * years (y) can use 1-4 placeholders, and milliseconds (s) can only use 1-3 digits
 5 * example:
 6  *(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 7  *(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 8  */
 9 Date.prototype.format = function (fmt) {
10     var o = {
11         "M+":  this.getMonth () + 1, // month
12         "d+":  this.getDate (), // day
13         "h+":  this.getHours (), // hours
14         "m+":  this.getMinutes (), // points
15         "s+":  this.getSeconds (), // seconds
16         "q+":  Math.floor (( this.getMonth () + 3) / 3), // quarter
17         "S":  this.getMilliseconds () // MS
18     };
19     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
20     for (var k in o)
21         if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
22     return fmt;
23 }

4. Page date 2017-06-27 changed to 20170627 “2017-06-27”. Replace (/ – / g, “).

Recommended Today

React source code Part7 – what’s behind reactdom.render?

summary This paper mainly introduces the process and differences of fiber update in react under different environmental modes React has three working modes Legacy mode: react17 and below temporarily use this mode by default, which is characterized by synchronous execution of tasks in the update queue Blocking mode: this mode is an excessive version of […]