JS date comparison, using regular expression Conversion

Source: Internet
Author: User

The format required by JavaScript For the date conversion function parse () is xxxx/XX/xx

Eg:

VaR RegEx = new Regexp ("\-", "Gi ");
Timehopestart = timehopestart. Replace (RegEx ,"/");

If (new date (date. parse (timehopestart) <new date ()){
Alert ("the expected start date must be greater than or equal to the current date! ");
Return false;
}

Online SectionCodeAs follows:

Window. onload = function (){
VaR dependedval = "2005-3-4 ";
// Convert a date string to a date
VaR RegEx = new Regexp ("\-", "Gi ");
Dependedval = dependedval. Replace (RegEx ,"/");
// Dependedval = dependedval. Replace ("\-", "/"); // This does not work
Alert (dependedval)
// The format 2005/3/4 is required for parse! Falk!
VaR milliseconds = date. parse (dependedval );
Alert (milliseconds)
VaR dependeddate = new date ();
Dependeddate. settime (milliseconds );

VaR now = new date ();
// Note the brackets. The priority is not correct.
Alert ("Years separated:" + (now. getfullyear ()-dependeddate. getfullyear ()));
}


Additional JS date knowledge:

--------------------------------------------------------------------

 

Create a date object:

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

The following five parameters are supported:

View plainnew date ("month DD, yyyy hh: mm: SS ");
New date ("month DD, YYYY ");
New Date (yyyy, MTH, DD, HH, mm, SS );
New Date (yyyy, MTH, DD );
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: Number of milliseconds, an integer greater than or equal to 0,It indicates the number of milliseconds between the creation time and the GMT time on January 1, January 1, 1970.

I found:

The construction of dates in javascript can also support new date ("yyyy/mm/DD"); where: mm isAn integer indicates the month.From 0 (January) to 11 (December), the regular expression can be used to easily convert the string date.

Test code:

<MCE: Script Type = "text/JavaScript"> <! --

Document. Write ("<br/>" + new date ("February 3, 2009 "));

Document. Write ("<br/>" + new date ("February 10:52:03 "));

Document. Write ("<br/> ");

Document. Write ("<br/>" + new date (2009, 1, 3 ));

Document. Write ("<br/>" + new date (2009, 1 ));

Document. Write ("<br/> ");

Document. Write ("<br/>" + new date (date. parse ("February 3, 2009 ")));

Document. Write ("<br/>" + new date (date. parse ("February 10:52:03 ")));

Document. Write ("<br/>" + new date (date. parse (2009, 1, 3); // output: Nan

Document. Write ("<br/>" + new date (date. parse (,); // output: Nan

Document. Write ("<br/>" + new date (date. parse ("2009/02/03 ")));

Document. Write ("<br/> ");

Document. Write ("<br/>" + new date ("2009/02/03 "));

Document. Write ("<br/>" + new date ("2009/02/03 11:12:13 "));

Document. Write ("<br/>" + new date ("2009-02-03"); // output: Nan



// --> </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

 

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.