On the basic knowledge of date and time in javascript

  • 2021-07-02 23:15:34
  • OfStack

Previous words

Before you introduce Date objects, you should first learn something about dates and times. For example, leap year, UTC and so on. A deeper understanding of these helps to better understand Date objects in javascript. This article will introduce the basic knowledge of javascript about date and time

Standard Time 1 Generally speaking, standard time refers to GMT and UTC, which used to be GMT and now is UTC

GMT

Greenwich Mean Time (GMT) is the standard time at the Royal Greenwich Observatory on the outskirts of London, because the prime meridian is defined as the meridian passing through it

In theory, noon GMT refers to the time when the sun crosses the Greenwich meridian (that is, when it is at its highest point over Greenwich). Due to the uneven velocity of the Earth in its elliptical orbit, this time may be 16 minutes away from the actual solar time

The Earth's daily rotation is somewhat irregular and is slowing down slowly. Therefore, Greenwich Mean Time is no longer used as standard time. The current standard time-Universal Coordinated Time (UTC) is provided

UTC

Universal Coordinated Time (UTC), also known as Universal Time, Universal Standard Time and Universal Coordinated Time, is a time measurement system based on atomic time and seconds, which is as close as possible to Universal Time in time

This time system has been used in many Internet and World Wide Web standards. The time difference between mainland China, Hong Kong, Macau, Taiwan, Mongolia, Singapore, Malaysia, Philippines and Western Australia and UTC is +8, that is, UTC +8

In the military, the Coordinated Universal Time Zone is represented by "Z". Since Z uses "Zulu" for radio communication, UTC will also be called "Zulu time"

Date time string format

ECMAScript defines a date-time string interchange format based on the simplified ISO 8601 extended format

The full date and time format is: YYYY-MM-DDTHH: mm: ss. sssZ

[Note] The prefix 0 cannot be omitted, otherwise an error will be reported in the case of complete format


YYYY     Gregorian middle-aged 10 Binary number, if the parameter value is in 0-99 Is added to it 1900
-       Directly in the string with " - " ( Dash ) Appear twice 
MM     1 The month of the year, from 01(1 Month ) To 12(102 Month )
DD      The date in the month, from the 01 To 31
T       Directly in the string with " T "Occurs to indicate the beginning of a time element 
HH      With two 10 Represented by binary numbers since midnight 0 Hours since 
:       Directly in the string with " : " ( Colon ) Appear twice 
mm      Is to use two 10 The number of minutes since the beginning of the hour, expressed by a binary number 
ss      Is to use two 10 The number of seconds since the beginning of the minute, expressed by a binary number 
.       Directly in the string with " . " ( Point ) Appear 
sss      Is to use 3 A 10 The number of milliseconds since the beginning of the second, expressed by a binary number 
Z       Is the time zone offset, which is determined by (" Z " ( Refer to UTC) Or " + "Or" - ") and the following time expression hh:mm Composition 

Date only format: YYYY YYYY-MM YYYY-MM-DD

[Note] All numbers must be decimal. If the MM or DD fields are missing, use "01" as their values. If mm or ss fields are missing, use "00" as their value, and for missing sss, use "000" as its value. Use "Z" for missing time zone offsets

Leap year

Years are divided into leap years and peace years, with 365 days in ordinary years and 366 days in leap years. February in leap years is one day longer than ordinary years

A leap year is defined as a year (divisible by 4) and (not divisible by 100) or (divisible by 400)

The formula is: 1 leap in 4 years, not leap in 100 years, and leap again in 400 years


function IsLeapYear(year){
  if(typeof year == 'number'){
    if((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0){
      return 'leap year'
    }else{
      return 'common year'
    }
  }
  return 'please input number'
}

console.log(IsLeapYear(4));//'leap year'
console.log(IsLeapYear(400));//'leap year'
console.log(IsLeapYear(2000));//'leap year'
console.log(IsLeapYear(1900));//'common year'

Month day

There are 12 months in a year, including 30 days in April, June, September and November; If it is a leap year, there are 29 days in February, otherwise, there are 28 days in February. There are 31 days in each month in January, March, May, July, August, October and December

In javascript, the calculation of month starts from 0, so January-December are represented by 0-11 respectively; The calculation of the day starts with 1, and 1 represents the first day, and so on


if(month == 2){
  // If it's a leap year 
  if((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0){
    days = 29;
  // If it is a normal year 
  }else{
    days = 28;
  }
// If it is the first 4 , 6 , 9 , 11 Month 
}else if(month == 4 || month == 6 ||month == 9 ||month == 11){
  days = 30;
}else{
  days = 31;
}

In javascript, the abbreviation of month is often used in the date string


1 Month     Jan January
2 Month     Feb February
3 Month     Mar March
4 Month     Apr April
5 Month     May May
6 Month     Jun June
7 Month     Jul July
8 Month     Aug August
9 Month     Sep September
10 Month     Oct October
101 Month    Nov November
102 Month    Dec December

Week

The week begins on Sunday and ends on Saturday, which is represented by 0-6 respectively

In javascript, the shorthand for each week is often used in the date string


 Sunday   sunday     Sun
 Week 1  monday     Mon
 Week 2  Tuesday    Tue
 Week 3  Wednesday   Wed
 Week 4  Thursday    Thu
 Week 5  Fridday    Fri
 Week 6  Saturday    Sar

Time and second


1 Days  = 24 Hours  = 24*60(1440) Points  = 24*60*60(86400) Seconds  = 86,400,000 Milliseconds 
  1 Points = 60 Seconds 
  1 Hours  = 3600 Seconds 
  1 Days  = 86400 Seconds 

The Date object returns a number of milliseconds, which often needs to be converted to hours and seconds


date = 100000s
day( Days ) = Math.floor(100000/86400) = 1
hour( Hours ) = Math.floor((100000%86400)/3600) = 3
minute( Points ) = Math.floor((100000%3600)/60) = 46
second( Seconds ) = Math.floor(100000%60)=40
console.log(100000 === 1*86400+ 3*3600+ 46*60+40);//true

Related articles: