Create a Date with a set timezone without using a string representation

前端 未结 23 1158
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 01:48

I have a web page with three dropdowns for day, month and year. If I use the JavaScript Date constructor that takes numbers, then I get a Date obje

23条回答
  •  心在旅途
    2020-11-22 01:56

    // My clock 2018-07-25, 00:26:00 (GMT+7)
    let date = new Date(); // 2018-07-24:17:26:00 (Look like GMT+0)
    const myTimeZone = 7; // my timeZone 
    // my timeZone = 7h = 7 * 60 * 60 * 1000 (millisecond);
    // 2018-07-24:17:26:00 = x (milliseconds)
    // finally, time in milliseconds (GMT+7) = x + myTimezone 
    date.setTime( date.getTime() + myTimeZone * 60 * 60 * 1000 );
    // date.toISOString() = 2018-07-25, 00:26:00 (GMT+7)
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题