UTC time is converted to normal time and compared to two time

Source: Internet
Author: User

Recently done project, encountered time conversion problem. Time control, the time format returned is in UTC format.

Next, because you have to do two time to compare. So I thought of a variety of ways.

Through the test, wrote a JS converter. To achieve conversion to normal format (YYYY-MM-DD)

The JS code is as follows: In fact, it is through the string array in the way of stitching into normal format.

function  convertdate (UTCSTR) {//tue May 5 0:00:00 utc+0800   "";  var date= "";//month  var month=new Array () month["Jan"]=01;month["Feb"]=02;month["Mar"]=03;month["APR"]=04; month["may"]=05;month["Jan"]=06;  month["Jul"]=7;month["]=8;month[" Sep "]=9;month[" Oct "]=10;month[" Nov "]=11;month[" Dec "]=12;//week var week=new Array ();  week["Mon"]= "one"; week["Tue"]= "two" week["Wed"]= "three" week["Thu"]= "four" week["Fri"]= "five" week["Sat"]= "Six" week["Sun"]= "Day ";//String Stitching  str=utcstr.split (" ");  date=str[5]+ "-";  2015-05-05 00:00:00 format  date=date+month[str[1]]+ "-" +str[2]+ "" +str[3 ";  2015-05-05 format  //date=date+month[str[1]]+ "-" +str[2];  Date=date+ "Week" +week[str[0]];  return date;}

The next step is to compare the size of the time in the two UTC format.

                var tcf=convertdate (DT.CFRQ). Split ('-');  var tdd=convertdate (DT.DDRQ). Split ('-');                    var sdate=new Date (tcf[0],tcf[1],tcf[2]);  var edate=new Date (tdd[0],tdd[1],tdd[2]);  if (Sdate.gettime () <=edate.gettime ())  {        return true;  }    else  {     alert (' Arrival date should be greater than or equal to the departure date ');     return false;  }

  

UTC time is converted to normal time and compared to two time

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.