About js datetime

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Convert a Date string such as "10:18:30" to a Date object
Var strArray = str. split ("");
Var strDate = strArray [0]. split ("-");
Var strTime = strArray [1]. split (":");
Var a = new Date (strDate [0], (strDate [1]-parseInt (1), strDate [2], strTime [0], strTime [1], strTime [2])

2: The second method is really simple.
Var s = "09:41:30 ";
Var d = new Date (Date. parse (s. replace (/-/g ,"/")));
----------------------------------------------------------------
To obtain the current time, refer:
Http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm
Http://www.quackit.com/javascript/tutorial/javascript_date_and_time.cfm

Var myDate = new Date ();
Var year = myDate. getYear (); // obtain the current year (2 digits)
Var year1 = myDate. getFullYear (); // obtain the complete year (four digits, 1970 -????)
Var moonth = myDate. getMonth (); // obtain the current month (0-11, 0 represents January)
MyDate. getDate (); // obtain the current day (1-31)
MyDate. getDay (); // obtain X of the current week (0-6, 0 indicates Sunday)
MyDate. getTime (); // obtain the current time (milliseconds starting from 1970.1.1)
MyDate. getHours (); // obtain the current hour (0-23)
MyDate. getMinutes (); // obtain the current number of minutes (0-59)
MyDate. getSeconds (); // obtain the current number of seconds (0-59)
MyDate. getMilliseconds (); // obtain the current number of milliseconds (0-999)
Var mytime = myDate. toLocaleDateString (); // get the current date // how is the English
Var mytime = myDate. toLocaleTimeString (); // obtain the current time
Var a = myDate. toLocaleString (); // obtain the date and time
--------------------------------------------
How to determine if it is of the datetime type in js?
1 short time, such as (13:04:06)
Copy codeThe Code is as follows:
Function isTime (str)
{
Var a = str. match (/^ (\ d {1, 2 })(:)? (\ D {1, 2}) \ 2 (\ d {1, 2}) $ /);
If (a = null) {alert ('input parameter is not in the timeformat '); return false ;}
If (a [1]> 24 | a [3]> 60 | a [4]> 60)
{
Alert ("Time Format incorrect ");
Return false
}
Return true;
}

2. Short date, such)
Copy codeThe Code is as follows:
Function strDateTime (str)
{
Var r = str. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /);
If (r = null) return false;
Var d = new Date (r [1], r [3]-1, r [4]);
Return (d. getFullYear () = r [1] & (d. getMonth () + 1) = r [3] & d. getDate () = r [4]);
}

3 long time, for example (13:04:06)
Copy codeThe Code is as follows:
Function strDateTime (str)
{
Var reg =/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d }) (\ d {1, 2}) :( \ d {1, 2}) :( \ d {1, 2}) $ /;
Var r = str. match (reg );
If (r = null) return false;
Var d = new Date (r [1], r [3]-1, r [4], r [5], r [6], r [7]);
Return (d. getFullYear () = r [1] & (d. getMonth () + 1) = r [3] & d. getDate () = r [4] & d. getHours () = r [5] & d. getMinutes () = r [6] & d. getSeconds () = r [7]);
}

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.