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

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

    I used the timezone-js package.

    var timezoneJS  = require('timezone-js');
    var tzdata = require('tzdata');
    
    createDate(dateObj) {
        if ( dateObj == null ) {
            return null;
        }
        var nativeTimezoneOffset = new Date().getTimezoneOffset();
        var offset = this.getTimeZoneOffset();
    
        // use the native Date object if the timezone matches
        if ( offset == -1 * nativeTimezoneOffset ) {
            return dateObj;
        }
    
        this.loadTimeZones();
    
        // FIXME: it would be better if timezoneJS.Date was an instanceof of Date
        //        tried jquery $.extend
        //        added hack to Fiterpickr to look for Dater.getTime instead of "d instanceof Date"
        return new timezoneJS.Date(dateObj,this.getTimeZoneName());
    },
    

提交回复
热议问题