The previous words
Before introducing the Date object, you must first understand some knowledge about dates and times. For example, leap years, UTC, etc. A deeper understanding of these will help you better understand the Date objects in javascript. This article will introduce the basics of javascript about dates and time
Standard time Generally speaking, standard time refers to GMT and UTC. It used to be GMT, but now it is UTC.
GMT
Greenwich Mean Time (GMT) refers to the standard time of the Royal Greenwich Observatory located in the suburbs of London, because the prime meridian is defined in the meridian through which
In theory, noon of GMT refers to the time when the sun crosses the Greenwich meridian (that is, at the highest point above Greenwich). Because the earth's movement speed in its elliptical orbit is uneven, this moment may be 16 minutes away from the actual sun time
The earth's rotation every day is somewhat irregular and is slowly decelerating. Therefore, Greenwich Time is no longer used as standard time. Current Standard Time - World Coordinated Time (UTC) provides
UTC
World Coordinated Time (UTC), also known as world unified time, world standard time, international coordinated time, full name Coordinated Universal Time, is a time measurement system based on the length of atomic time and is as close as possible to the world time at the moment.
This time system is applied to many Internet and World Wide Web standards. The time difference between time and UTC in mainland China, Hong Kong, Macau, Taiwan, Mongolia, Singapore, Malaysia, the Philippines, and Western Australia is +8, that is, UTC+8
In the military, the coordinated world time zone is represented by "Z". Since Z uses "Zulu" as a synonym for radio contact, it is also called "Zulu time" when coordinating the world.
Date time string format
ECMAScript defines a string interchange format based on datetime in the simplified ISO8601 extension format
The complete format of date and time is: YYYY-MM-DDTHH:mm:ss.sssZ
[Note] The pre-0 cannot be omitted, otherwise an error will be reported in the case of the full format.
YYYY The decimal number in the middle age of the Gregorian calendar. If this parameter value is between 0-99, add 1900 to it-in-in the string directly as "-" (dash) MM Month of the year, from 01 (January) to 12 (December) DD Date in the month, from 01 to 31T directly as "T" in the string, to indicate the beginning of the time element HH, represented by two decimal numbers, hours since midnight: directly as ":" (colon) mm is represented by two decimal numbers, minutes since the beginning of the hour ss is represented by two decimal numbers, seconds since the beginning of the minute. Directly as "." (dot) in the string, sss is represented by three decimal numbers, milliseconds since the beginning of the second Z is a time zone offset, composed of ("Z" (referring to UTC) or "+" or "-") and the time expression hh:mm followed byFormat that only represents date: YYYYYY-MM YYYY-MM-DD
[Note] All numbers must be in decimal. If MM or DD fields are missing, use "01" as their values. If the mm or ss fields are missing, use "00" as their values and "000" as their values for missing sss. Use "Z" for missing time zone offset
leap year
Years are divided into leap year and peaceful years, with 365 days in a leap year and 366 days in a leap year. February is one more than a normal year.
The definition of a leap year is a year that is (dividable by 4) and (dividable by 100) or (dividable by 400))
The formula is: leap every four years, no leap every hundred years, and leap every four hundred 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'
Monthly Day
There are 12 months in a year, of which 4, 6, 9 and November have 30 days per month; if it is a leap year, there are 29 days in February, otherwise, there are 28 days in February. 1, 3, 5, 7, 8, 10, and December have 31 days per month
In JavaScript, the calculation of the month starts from 0, so January-December is represented by 0-11 respectively; while the calculation of the day starts from 1, 1 represents the first day, and so on
if(month == 2){ //If it is a leap year if((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0){ days = 29; //If it is a common year}else{ days = 28; }//If it is the 4th, 6th, 9th, and November}else if(month == 4 || month == 6 ||month == 9 ||month == 11){ days = 30;}else{ days = 31;}In javascript, the abbreviation of the month is often used in date strings
January Jan January February February February Mar March April April May May May May June June July July August August August September Sep September October October October October Nov November December Dec December
Week
Weeks start on Sunday and end on Saturday, respectively, representing 0-6.
In javascript, the abbreviation of each week is often used in date strings
Sunday Sunday Monday Monday Monday Monday Tuesday Tuesday Tuesday Tuesday Tuesday Wednesday Wednesday Wednesday Wednesday Thursday Thu Friday Fri Saturday Saturday Sar
Time, minute, second
1 day = 24 hours = 24*60(1440) minutes = 24*60*60(86400) seconds = 86,400,000 milliseconds 1 minute = 60 seconds 1 hour = 3600 seconds 1 day = 86400 seconds
The Date object returns a number of milliseconds, which often needs to be converted into the form of time, minute and second.
date = 100000sday(day) = Math.floor(100000/86400) = 1hour(hour) = Math.floor((100000%86400)/3600) = 3minute(minutes) = Math.floor((100000%3600)/60) = 46second(seconds) = Math.floor(100000%60)=40console.log(100000 === 1*86400+ 3*3600+ 46*60+40);//true
The above brief discussion on the basic knowledge about dates and time in JavaScript is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.