JavaScript Date-Time representations cross-turn

Source: Internet
Author: User
Tags local time

In a language, a DateTime must have 3 representations, a string (such as 2010-11-11 05:07:08), a long (usually the number of milliseconds since January 1, 1970), and a built-in object (the date in JavaScript);

So the date time operation is a very common one is the above three manifestations of the mutual transfer and formatting (strings in various formats);
Moment is a date-time library of JavaScript that makes it easy to implement the formatting of the interactive and string representations of the three representations described above.

Three representations of datetime types, strings are read, long is faster when passing information, and built-in objects play a role in transit.

1. String to built-in object

new  date  (Moment (,  "Yyyy-mm-dd HH:mm:ss" )) new  date  (Moment ( "12-25-1995" ,  "mm-dd-yyyy" )) new  date  (Moment (, [" mm-dd-yyyy ", " dd-mm ", Span class= "hljs-string" > "dd-mm-yyyy" ]) new  date  (Moment ( "2010-01-01t05:06:07" , moment. iso_8601))  

The reason why you can turn this way:
The ToString method of moment returns a "ddd MMM DD YYYY HH:mm:ss [Gmt]zz", a string of this format that is exactly the default return format of the Date object, so the date object can recognize the string. As follows:

2.Long-Turn built-in objects
This function is generally the language of the built-in object to bring, as a built-in object of the existence of a method.

Both of these operations can turn the time string and the long type into a built-in object in any format, and the following is the return of the built-in object.

3. The built-in object is converted to any format string
In fact, the date object has eight built-in methods for outputting to a string format, respectively:
1) toString () to convert the Date object to a string. The format is as described in 1.
2) totimestring (), converts the time portion of a Date object to a string, formatted as described in 1, takes the time portion.
3) todatestring (), converts the date part of a Date object to a string, formatted as described in 1, and takes the date part.
4) togmtstring (), Get the GMT string
5) toUTCString (), converts the Date object to a string according to the universal.
6) tolocalestring () converts the Date object to a string based on the local time format.
7) tolocaletimestring () converts the time portion of a Date object to a string, based on the local time format.
8) tolocaledatestring () converts the date part of a dates object to a string, based on the local time format.
Although the JavaScript Date object has built-in methods to provide these output as strings, these strings are not in our control format, so what if we need our own custom special format?
Moment provides a powerful format function that can display the time as an arbitrary legal format;

moment(newDate()).format("dddd, MMMM Do YYYY, h:mm:ss a"); moment(newDate()).format("ddd, hA");  

4. Built-in object to long type
As with the long type into the built-in object, this is generally the function provided by the built-in object. JavaScript is no exception, the date built-in object provides a gettime method that returns the number of milliseconds since January 1, 1970. The date also has a static method of UTC (), which can return January 1, 1970 to a specified number of milliseconds according to the time of the day, with the following syntax

Date.UTC(year,month,day,hours,minutes,seconds,ms)

Okay, it can actually end here.
However, moment, as a heavyweight JavaScript datetime library, provides a powerful date-time Operation feature. It is actually a wrapper over the date of the JavaScript built-in object, so it can be used instead of the transit role of the Date object to achieve the mutual transfer of strings and long types.
5. Arbitrary string to long type
We know that the gettime of the Date object returns the number of seconds since the start of the January 1, 1970 (Midnight of Utc/gmt), which is actually the UNIX timestamp calculation, so moment provides a function called Unix that takes a long value of a time directly.
With this function, plus the powerful formatting capabilities of moment itself, you can convert any string directly to a long type, as follows:

moment("2010-11-11 05:07:08","Yyyy-mm-dd HH:mm:ss").Unix()moment("12-25-1995", "mm-dd-yyyy").Unix()moment("29-06-1995", ["mm-dd-yyyy", "dd-mm", "dd-mm-yyyy"]).Unix()moment("2010-01-01t05:06:07", moment.  iso_8601).Unix()

Type 6.Long to arbitrary string
In 3, you have seen the format function that formats the output string as any valid format, so it okay. As follows:

moment(1000000).format("dddd, MMMM Do YYYY, h:mm:ss a")moment(10000000000).format("YYYY-MM-DD hh:mm:ss a");  

Summary: You only need to look at 5 and 6 on it.

JavaScript Date-Time representations cross-turn

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.