简体   繁体   中英

Converting UTC date to mm/dd/yyyy

I am having a date in isoUtc format and I want to convert it into mm/dd/yyyy format. I tried to use the hint given in this blog entry but the problem I am facing is that if I convert 2007-04-06T00:00Z it gives different dates when user time zone is different. I want that it should give 04/06/2007 always independent of the user timezone.

Any help is appreciated

You could thy this, if you have always constant format:

var dateString = '2007-04-06T00:00Z',
    dateRegExp = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/,
    match = dateString.match(dateRegExp),
    date;

if (match) {
  date = new Date(match[1], match[2] - 1, match[3], match[4], match[5]);

  console.log(date);
}

DEMO

var d = '2007-04-06T00:00Z';
var d2 = d.substring(5,7)+'/'+d.substring(8,10)+'/'+d.substring(0,4);
// outputs 04/06/2007

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM