如何在Javascript上将时间字符串转换为Date对象?

时间:2017-04-26 11:49:20

标签: javascript

我有一个这样的字符串 - " 05:21 PM" 。如何将其转换为当前时区的日期对象?

1 个答案:

答案 0 :(得分:0)

.........

https://www.w3schools.com/jsref/jsref_sethours.asp

var d = new Date();
d.setHours(15);
d.setMinutes(15);

toLocalTime(d.getTime())

.........

/**
 * Converts CDT time to your browser local time.
 * @param time
 * @returns
 */
function toLocalTime(time) {
    if (time <= 0)
        return '';

    var m = moment.tz(time, 'America/Chicago'); //CDT

    var tz = jstz.determine(); // Determines the time zone of the browser client

    m.tz(tz.name()); // Convert CDT to local time

    return m.format('HH:mm:ss');
}