Basic knowledge of date and time in JavaScript

Source: Internet
Author: User
Tags time zones string format time and seconds

Front.

Before you introduce the Date object, first understand some knowledge about dates and times. For example, leap years, UTC, and so on. Having a closer look at these will help you better understand the date objects in JavaScript. This article will introduce the basics of JavaScript about date and time

Standard time generally refers to GMT and UTC, formerly GMT, and now UTC

Gmt

Greenwich Mean Time (GMT) is the standard time for the Royal Greenwich Observatory, located on the outskirts of London, because the meridian is defined through the warp

In theory, the midday of Greenwich Mean time is the time when the Sun crosses the Greenwich meridian (that is, the highest point over Greenwich). Because the earth's velocity in its elliptical orbit is uneven, this moment may be 16 minutes from the actual sun.

The daily rotation of the earth is somewhat irregular, and it is slowing down slowly. So, GMT is no longer used as a standard time. Now the standard time-world coordinated Time (UTC) provides

Utc

World coordinated Time (UTC) is also called World unification Time, world standard Time, international coordination time, full name Coordinated Universal Time, is a kind of measuring system based on atom time and seconds, and as close to the world as possible at all times.

This time system is used in many Internet and world Wide Web standards, China, Hong Kong, China, Macao, China, Taiwan, Mongolia, Singapore, Malaysia, the Philippines, State Of Western Australia time and UTC are +8, that is, utc+8

In military, coordinated world time zones are represented by "Z". And since Z uses "Zulu" as a surrogate in radio communications, it is also known as "Zulu time" when coordinating the world.

Date Time string format

ECMAScript defines a string interchange format for date-time based on simplified ISO8601 extended format

Date Time full format is: YYYY-MM-DDTHH:MM:SS.SSSZ

[note] Front 0 cannot be omitted, otherwise the error will be in full format

YYYY    Gregorian decimal digit, if this parameter value is between 0-99, then add 1900 to it
-in the      string directly to "-" (dash) appear two
MM     in a year, from 01 (January) to 12 (December)
the     date in the DD month, which
appears directly in the      string as "T" from 01 to T, to indicate the beginning of the time element
HH     in two decimal digits, the number of hours since 0 o'clock midnight
:      in the string directly with ":" (colon) appears two times
mm     is in two decimal digits, the number of minutes since the beginning of the Min
SS     is in two decimal digits, since the beginning of the number of seconds
.      Directly in the string with the "." (dot) appears
SSS     is represented in three decimal digits, and the number of milliseconds since the start of the second
Z      is the time zone offset, consisting of ("Z" (UTC) or "+" or "-") and followed by a time expression hh:mm

Format for date only: YYYY yyyy-mm yyyy-mm-dd

[note] All numbers must be 10-in. If the MM or DD field is missing, use "01" as their value. If the MM or SS fields are missing, use "00" as their value, and "000" as its value for the missing SSS. For missing time zone offsets with "Z"

Leap

Year is divided into years of peace year, excepting has 365 days, leap years have 366 days, leap February more than excepting one day

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

The formula is: four years a leap, hundred years does not leap, 400 year again leap

function Isleapyear {
  if (typeof year = = ' number ') {if (year
    % 4 = 0 && Year%!== 0) | | = = = 0) {return
      ' Leap year '
    }else{return
      ' Common Year '
    } return ' please input number
  '
}
Console.log (Isleapyear (4));//' Leap year '
Console.log (isleapyear);//' Leap year '
Console.log ( Isleapyear);//' Leap year '
Console.log (Isleapyear (1900));//' Common year '

Month Day

There are 12 months in a year, of which 4, 6, 9, and November are 30 days per month, or 29 days in February if they are leap years, or 28 days in February. 1, 3, 5, 7, 8, 10, December 31 days per month

In JavaScript, the month calculation starts at 0, so January-December is represented by 0-11, while the day calculation starts at 1, 1 on the 1th day, and so on.

if (month = = 2) {
  //If it is a leap year if (
  [year% 4 = 0 && Year%!== 0) | | |-year% = = 0] {days
    = MB;
  If it is excepting
  }else{days
    =;
  }
If the 4th, 6, 9, November
}else if (month = 4 | | month = 6 | | Month = 9 | | Month = = (=) {days
  =;
} else{days
  =;
}

In JavaScript, the abbreviations for months are often used in date strings

January    January
February    Feb February
March    Mar March
April    Apr April
May    May
June    June June
July    July
August    Aug August
September    Sep September
October    Oct October
November   Nov November
December   Dec December

Week

The week begins in Sunday and ends in Saturday, and is expressed in 0-6 respectively.

In JavaScript, the abbreviations for each week are often used in date strings

Sunday  Sunday     Sun
Monday  Monday     Mon
Tuesday  Tuesday    Tue
Wednesday  Wednesday   Wed
Thursday  Thursday    Thu
Friday  fridday    Fri
Saturday  Saturday    Sar

Time and seconds

1 days = 24 hours = 24*60 (1440) min = 24*60*60 (86400) sec = 86,400,000 Millisecond
  1 min = 60 Second
  1 hour = 3,600 second
  1 days = 86,400 seconds

The Date object returns a number of milliseconds and often needs to be converted to a time and seconds form

Date = 100000s
Day (days) = Math.floor (100000/86400) = 1
Hour (hours) = Math.floor ((100000%86400)/3600) = 3
minute (min = Math.floor ((100000%3600)/60) =
second (sec) = Math.floor (100000%60) =40 console.log
(100000 = = 1*86400+ 3* 3600+ 46*60+40);//true

The above article on the basics of the date and time in JavaScript is a small series to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.