Javascript: conversion between a date string (datestr) and a date object (date)

Source: Internet
Author: User

I am working on a project today and have encountered a need to convert a date string (for example,) into a date object in Javascript. I am confused about the lack of JavaScript knowledge and the four parts, I checked some information online and got it out...

 

Go directly to the topic:

Create a date object:

VaR objdate = new date ([arguments list]);

 

The following five parameters are supported:
New date ("month DD, yyyy hh: mm: SS"); <br/> new date ("month DD, YYYY"); <br/> new date (yyyy, MTH, DD, HH, mm, SS); <br/> new date (yyyy, MTH, DD); <br/> new date (MS );

Note:

Month: represents the name of a month in English, from January to December.
MTH: indicates the month in an integer, from 0 (January 1, January) to 11 (January 1, December)

Content


DD: the day of a month, from 1 to 31.
Yyyy: The Year in four-digit format.
Hh: hours, from 0 (midnight) to 23)
MM: The number of minutes, an integer from 0 to 59.
SS: number of seconds, an integer from 0 to 59
MS: The number of milliseconds. The value is an integer greater than or equal to 0, indicating the difference between the creation time and GMT time on January 1, January 1, 1970.

 

I found:

The construction of dates in Javascript also supports the new date ("yyyy/mm/DD"), where: mm is an integer representing the monthFrom 0 (January) to 11 (December), the regular expression can be used to easily convert the string date.

 

Test code:

<MCE: Script Type = "text/JavaScript"> <! -- <Br/> document. write ("<br/>" + new date ("February 3, 2009"); <br/> document. write ("<br/>" + new date ("February 10:52:03"); <br/> document. write ("<br/>"); <br/> document. write ("<br/>" + new date (2009,1, 3); <br/> document. write ("<br/>" + new date (2009, 1,); <br/> document. write ("<br/>"); <br/> document. write ("<br/>" + new date (date. parse ("February 3, 2009"); <br/> document. write ("<br/>" + new date (date. parse ("February 10:52:03"); <br/> document. write ("<br/>" + new date (date. parse (2009, 1, 3); // output: Nan <br/> document. write ("<br/>" + new date (date. parse (2009, 1,); // output: Nan <br/> document. write ("<br/>" + new date (date. parse ("2009/02/03"); <br/> document. write ("<br/>"); <br/> document. write ("<br/>" + new date ("2009/02/03"); <br/> document. write ("<br/>" + new date ("2009/02/03 11:12:13"); <br/> document. write ("<br/>" + new date ("2009-02-03"); // output: Nan </P> <p> // --> </MCE: SCRIPT>

Output result:

Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 10:52:03 UTC + 0800 2009

Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 10:52:03 UTC + 0800 2009

Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 10:52:03 UTC + 0800 2009
Nan
Nan
Tue Feb 3 00:00:00 UTC + 0800 2009

Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 11:12:13 UTC + 0800 2009
Nan

 

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.