Javascript DateFormat for different timezones

后端 未结 9 2044
小蘑菇
小蘑菇 2020-12-16 16:42

I\'m a Java developer and I\'m used to the SimpleDateFormat class that allows me to format any date to any format by settings a timezone.

Date date = new Dat         


        
相关标签:
9条回答
  • 2020-12-16 17:14

    I think the accepted answer is not correct. There is a way to format for time zones.

    console.log(new Date().toLocaleDateString('en-US', {timeZone: 'America/Denver'}))
    // 11/13/2018
    console.log(new Date().toLocaleTimeString('en-US', {timeZone: 'America/Denver'}))
    // 2:30:54 PM
    console.log(new Date().toLocaleTimeString('en-US', {timeZone: 'America/New_York'}))
    // 4:31:26 PM
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString

    0 讨论(0)
  • 2020-12-16 17:14

    If you're just passing the raw TZ there's nothing really complicated about adjusting the hours. My example below is of course abbreviated. Yours may get quite long depending on how many patterns you'd handle.

    Date.prototype.format = function(format, tzAdjust) {
        // adjust timezone
        this.setHours(this.getHours()+tzAdjust)
        // pad zero helper - return "09" or "12"
        var two = function(s){ return s+"".length==1 ? "0"+s : s+""; }
        // replace patterns with date numbers
        return format.replace(/dd|MM|yyyy|hh|mm|ss/g, function(pattern){
            switch(pattern){
                case "d" : return this.getDate();
                case "dd" : return two(this.getDate());
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-16 17:16

    You are clearly asking two questions in one, formatting and time zone. They need to be addressed separately. Formatting is pretty trivial, if none of the other answers will do for that you will have to be more specific.

    As for the time and time zone, if you have your server inject the UTC time, preferably as UNIX time in milliseconds, into the JavaScript, you can compare that to the time on the client machine, and thus work out how far from UTC the client is. Then you can calculate the time of any time zone you want.

    Edit: I actually didn't know JavaScript also had built in UTC time until I checked on the internet, neat.

    In any case, I suppose this is want you want:

    Date.prototype.format=function(format,timezone){
        var obj=new Date(this.getTime()+this.getTimezoneOffset()*60000+timezone*3600000);
        var two=function(s){
            return s<10?"0"+s:s+"";
        }
        return format.replace(/dd|MM|yyyy|hh|mm|ss/g, function(pattern){
            switch(pattern){
                case "dd" : return two(obj.getDate());
                case "MM" : return two(obj.getMonth()+1);
                case "yyyy" : return obj.getFullYear();
                case "hh" : return two(obj.getHours());
                case "mm" : return two(obj.getMinutes());
                case "ss" : return two(obj.getSeconds());
            }
        });
    }
    

    You can add in more patterns if you need.

    0 讨论(0)
提交回复
热议问题