简体   繁体   中英

How to convert string to appropriate Date format javascript

I have strings of date like this:

Wed 02/27 - 10PM PST

Thu 02/28 - 1PM PST

How to convert it to appropriate javascript Date format?

> new Date(Date.parse("Thu, 01 Jan 1970 10:00 GMT-0400"))
Thu Jan 01 1970 15:00:00 GMT+0100 (CET)

With a little string manipulation you should be good to go.

var match = /.+? (\d{2})\/(\d{2}) - (\d+)([AP]M) PST/.exec(someDateString);
if (!match)
    return new Date(NaN); // or something else indicating a parse error
var year = 2013,
    month = parseInt(match[1], 10) - 1; // zero-based
    day = parseInt(match[2], 10);
var hour = parseInt(match[3], 10) + (match[3] == "PM" ? 12 : 0);
    hour += 8; // adjust from PST to UTC
return new Date(Date.UTC(year, month, day, hour));

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