About JS datetime of that thing _javascript skill

Source: Internet
Author: User
Tags getdate
Copy Code code as follows:

Converts a date string such as "2007-2-28 10:18:30" to the 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 easy.
var s = "2005-12-15 09:41:30";
var d = new Date (Date.parse (S.replace (/-/g, "/"));
----------------------------------------------------------------
Get the current time reference:
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 (); Get Current year (2-bit)
var year1=mydate.getfullyear (); Get full year (4-bit, 1970-????)
var moonth=mydate.getmonth (); Get Current month (0-11, 0 for January)
Mydate.getdate (); Get the current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (number of milliseconds since 1970.1.1)
Mydate.gethours (); Get the current number of hours (0-23)
Mydate.getminutes (); Get the current number of minutes (0-59)
Mydate.getseconds (); Get the current number of seconds (0-59)
Mydate.getmilliseconds (); Get current number of milliseconds (0-999)
var mytime=mydate.tolocaledatestring (); Get the current date//How is English
var mytime=mydate.tolocaletimestring (); Get current time
var a=mydate.tolocalestring (); Get Date and time
--------------------------------------------
How to determine whether a datetime type in JS
1 short time, form (13:04:06)
Copy Code code 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 a time format '); return false;}
if (a[1]>24 | | a[3]>60 | | a[4]>60)
{
Alert ("Time format is not");
return False
}
return true;
}

2. Short date, form (2008-07-22)
Copy Code code as follows:

function Strdatetime (str)
{
var r = Str.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
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, shaped like (2008-07-22 13:04:06)
Copy Code code as follows:

function Strdatetime (str)
{
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\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]);
}

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.