Introduction to JavaScript Date objects and how Linux timestamps are converted into standard time formats in JavaScript

Source: Internet
Author: User
Tags local time

1.Date Object Introduction

A Date object has multiple constructors.
NewDate ()
NewDate (milliseconds)
new date (datestring)
new date (year, month)
new date (year, month, day)
new< Span style= "color: #000000;" > date (year, month, day, hours)
new  date (year, month, day, hours, minutes)
new date (year, month, day, hours, minutes,  seconds)
new date (year, month,  day, hours, minutes, seconds, microseconds)

If there are no arguments, the current date is returned
If the argument is a number, the number is treated as a millisecond value and converted to a date
If the argument is a string, the string is treated as a string representation of the date, converted to a date

We can use Date.parse () and DATE.UTC () to get the number of milliseconds for a custom time.

Date.parse () receives a string parameter that represents a date, such as "may 25,2013", "6/13/2013" and other formats, depending on the locale.

The parameters that are received in DATE.UTC () are the year, the number of months starting from 0 (0-11), the Day of the month (1-31), The Hour Tree (0-23), the minute, the second, and the milliseconds, where the year and the number of months are mandatory, and the other parameters are 0 by default.

Now, if we want to define a date object for December 12, 2013, you can use Date.parse ():

var mydate=new Date (Date.parse ("12/12/2013")),

If so:

var mydate=new date ("12/12/2013"), when the date is constructed, the Date.parse () is automatically called to convert the day string to the number of milliseconds.

You can also use DATE.UTC ():

var mydate=new date (DATE.UTC (2013,11,12)//Note month subscript starting from 0, and day subscript starting from 1

If so:

var mydate=new date (2013,11,12), similar to the constructor method of the above parse, will automatically call DATE.UTC () when constructing the Date object, if the first parameter is a numeric value, it is the year, and the second argument is the month ... However, it is important to note that the former var mydate=new date (DATE.UTC (2013,11,12) is GMT time, the latter var mydate=new date (2013,11,12) is based on the system sets the local time.

2.Linux timestamp converted to standard time format

There are many occasions in a Linux system that use timestamps to represent time, that is, from January 1, 1970 to the current number of seconds.

About the interpretation of the time format

UTC (Universal time COORDINATED,UTC) world coordination Times

CST China Time utc+8:00 (Beijing time)

GMT (Greenwich Mean Time) GMT: Here's how the Linux timestamp translates into a standard time format under javascript:
1  functionStandarddateformat (timestamp)2     {3         varDate =NewDate (timestamp*1000);//Timestamp is the number of seconds, and the Date () parameter requires milliseconds, so multiply by4 5Y = date.getfullyear () + '/';6M = (Date.getmonth () +1 < 10? ' 0 ' + (date.getmonth () +1): Date.getmonth () +1) + '/';7D = (Date.getdate () < 10? ' 0 ' +date.getdate (): Date.getdate ()) + "";8H = (Date.gethours () < 10? ' 0 ' +date.gethours (): Date.gethours ()) + ': ';9m = (date.getminutes () < 10? ' 0 ' +date.getminutes (): Date.getminutes ()) + ': ';Tens = (Date.getseconds () < 10? ' 0 ' +date.getseconds (): Date.getseconds ()); One         return(y+m+d+h+m+s); A}

Introduction to JavaScript Date objects and how Linux timestamps are converted into standard time formats in JavaScript

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.