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

前端 未结 23 1116
爱一瞬间的悲伤
爱一瞬间的悲伤 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 02:19

    This worked for me. Not sure if it is a good idea though.

    var myDate = new Date();
    console.log('myDate:', myDate);   // myDate: "2018-04-04T01:09:38.112Z"
    
    var offset = '+5';  // e.g. if the timeZone is -5
    
    var MyDateWithOffset = new Date( myDate.toGMTString() + offset );   
    
    console.log('MyDateWithOffset:', MyDateWithOffset); // myDateWithOffset: "2018-04-03T20:09:38.000Z"

提交回复
热议问题