This is the 20th day of my participation in the August Text Challenge.More challenges in August

At that time there was such a pit, in the local debugging is no problem, debugging on android phones is no problem, but, it happened to ios, how can not get, has been wrong judgment, later found a lot of hair, finally found that the original, ios processing time format has rules. Ios cannot parse the time in this format: 2021-08-21 23:00:00, it needs to change the time to 2021/08/21 23:00:00, but the time returned by the back end is in this format, so we have to deal with the front end itself

if (ios) {
   time = time.replace(/-/g."/");
}
Copy the code

The ISO 8601 standard for strings makes a magical assertion: all strings that do not provide a time zone default to the standard time zone. In other words, you’ll find that the time you parse is different from what you expected, and it will be converted to the local time zone when printing. ES 2015 fixes this Bug, but it’s still a bit of a headache, because you don’t know whether your code will end up running on ES5 or ES6.

I remember that there was a need to deal with a lot of time, so I learned a lot about the operation of time

time

create

There are four ways to create it

var today = new Date(a)// If no arguments are provided, the newly created Date object represents the Date and time of the instantiation moment. Sat Aug 21 2021 23:25:24 GMT+0800
var d1 = new Date("October 13, 1975 11:13:00") // Mon Oct 13 1975 11:13:00 GMT+0800
var d2 = new Date(79.5.24) // Sun Jun 24 1979 00:00:00 GMT+0800
var d3 = new Date(79.5.24.11.33.0) // Sun Jun 24 1979 11:33:00 GMT+0800
Copy the code

To obtain

let dt = new Date()

Years: getFullYear ()

Month: getMonth ()

Day: getDate ()

When: getHours ()

Points: getMinutes ()

Second: getSeconds ()

Ms: the getMilliseconds ()

Milliseconds from January 1, 1970 to now: getTime()

Set up the

Years: setFullYear ()

Month: setMonth ()

Day: settDate ()

When: setHours ()

Points: setMinutes ()

Second: setSeconds ()

Ms: setMilliseconds ()

Year Month Day hour Minute second format YYYY-MM-DD HH: MM: SS

example
At that time, we need to get the first day of the current day
var date = new Date(), 
y = date.getFullYear(),
m = date.getMonth();
var firstDay = new Date(y, m, 1);
Copy the code
How to format a date into a specified format?

You see some of the processing on the Internet, as long as we have these functions, we don’t have to spend so much time to deal with the processing

Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1./ / in
    "d+": this.getDate(),                        / /,
    "h+": this.getHours(),                       / / hour
    "m+": this.getMinutes(),                     / / points
    "s+": this.getSeconds(),                     / / SEC.
    "q+": Math.floor((this.getMonth() + 3) / 3), / / quarter
    "S": this.getMilliseconds()                  / / ms
  };

  // Get the year
  / / 1.
  if (/(y+)/i.test(fmt)) {
    fmt = fmt.replace(RegExp. $1,this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }

  for (var k in o) {
    / / 2.
    if (new RegExp("(" + k + ")"."i").test(fmt)) {
      fmt = fmt.replace(
        RegExp. $1,RegExp.$1.length == 1)? (o[k]) : (("00" + o[k]).substr((""+ o[k]).length))); }}return fmt;
}

var now = new Date(a);var nowStr = now.format("YYYY-MM-DD"); / / 2021-01-11
Copy the code