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

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

    var d = new Date(xiYear, xiMonth, xiDate);
    d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
    

    This answer is tailored specifically to the original question, and will not give the answer you necessarily expect. In particular, some people will want to subtract the timezone offset instead of add it. Remember though that the whole point of this solution is to hack javascript's date object for a particular deserialization, not to be correct in all cases.

提交回复
热议问题