JavaScript string types are converted to date types in several ways

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax array date document javascript return split start

If there is a space in the string or other segmentation, you can split the Split method, return an array, and then the array of years as a parameter to new date () to generate date type;
If there is no special separator, you can use the substr (X,y) method to take the substring to generate the date, substr (X,y), X represents the start of the string, y to the length of the substring!
If the string is "/" split, it can be converted using Date.parse (DATESTR).

Look at the example with "-" separate

var checkstartdate = Document.form1.checkstartdate.value;
var checkenddate = Document.form1.checkenddate.value;
var arys= new Array ();
var startdate=new date (Arys[0],parseint (arys[1]-1), arys[2]);
if (checkstartdate!= null && checkenddate!= null) {
Arys=checkstartdate.split ('-');
var startdate=new date (Arys[0],parseint (arys[1]-1), arys[2]);
Arys=checkenddate.split ('-');
var checkenddate=new date (Arys[0],parseint (arys[1]-1), arys[2]);
if (StartDate > Checkenddate) {
Alert ("Your check date begins more than the end Time");
Return;
}
}


Take another look.

var nowdate = getdate ()//Get system time
   var shendate = document.getElementById ("Billdate"). Value;
   var   arr=nowdate.split ("");  
   var   arr1=arr[0]. Split ("-");  
   var   arr2=arr[1].split (":");
   var nowdate1 = new Date (arr1[0],arr1[1],arr1[2],arr2[0],arr2[1],arr2[2]);

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.