Javascript DateFormat for different timezones

后端 未结 9 2043
小蘑菇
小蘑菇 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 16:55

    Since my requirement was a typescript solution but I stumbled here I used this answer to write my typescript function.

    Based on answer above, a function in typescript which converts a timestamp or a date object into a formatted local time string.

    const formatDateString = (date_or_ts:Date|number):string=>{
        let obj:Date;
    
        if(typeof date_or_ts === "number"){
            obj = new Date(date_or_ts*1000);
            // obj=new Date(obj.getTime()+obj.getTimezoneOffset()*60000+timezone*3600000);
        }else{
            obj = date_or_ts;
        }
        const format = "dd-MM-yyyy hh:mm:ss";
        let two=function(s:number){
            return s<10?"0"+s:s+"";
        }
        return format.replace(/dd|MM|yyyy|hh|mm|ss/g, function(pattern){
            const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
            switch(pattern){
                case "dd" : return two(obj.getDate()).toString();
                case "MM" : return months[obj.getMonth()];
                case "yyyy" : return obj.getFullYear().toString();
                case "hh" : return two(obj.getHours()).toString();
                case "mm" : return two(obj.getMinutes()).toString();
                case "ss" : return two(obj.getSeconds()).toString();
                default: return "";
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-16 16:56

    This is an old question, but since I found it:

    As mentioned, there's nothing reasonable built-in.

    As for libs, there is Moment Timezone for Moment.js.

    Here is a JSfiddle with an example: http://jsfiddle.net/kunycrkb/

    The same code inline:

    var m = moment("2014-06-01T13:05:00Z");
    var f = "HH:mm z";
    
    $("#results").text(m.tz("UTC").format(f) + " is " + m.tz("EST").format(f) +  "!");
    
    0 讨论(0)
  • 2020-12-16 16:57

    JavaScript does not have build in support for other time zone than the local one. You can only express a date in local time or in UTC time. There is no way to change the time zone offset of a Date object.

    Thus, there is no "neat" way to solve your problem.

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

    Don't write your own stuff; just get datejs: http://www.datejs.com/

    You can figure out what the timezone offset is set to in the execution environment like this:

    var local = new Date();
    var utc = Date.UTC(local.getFullYear(), local.getMonth(), local.getDate(), local.getHours(), local.getMinutes(), local.getSeconds(), local.getMilliseconds());
    var tz = (utc - local.getTime()) / (60 * 60 * 1000);
    
    0 讨论(0)
  • 2020-12-16 17:09

    Attempting to (ever so slightly) improve upon mwilcox's suggestion:

    Date.prototype.format = function(format, tzAdjust) {
    
        // get/setup a per-date-instance tzDate object store
        var tzCache = this.__tzCache = this.__tzCache || (this.__tzCache = {});
    
        // fetch pre-defined date from cache 
        var tzDate = tzCache[tzAdjust];
        if ( !tzDate )
        {
          // on miss - then create a new tzDate and cache it
          tzDate = tzCache[tzAdjust] = new Date( this );
          // adjust by tzAdjust (assuming it's in minutes 
          // to handle those weird half-hour TZs :) 
          tzDate.setUTCMinutes( tzDate.getUTCMinutes()+tzAdjust );
        }
    
        return format.replace(/dd|MM|yyyy|hh|mm|ss/g, function(pattern){
                   // replace each format tokens with a value 
                   // based on tzDate's corresponding UTC property
                 });
    }
    
    0 讨论(0)
  • 2020-12-16 17:10

    The ISO Extended format for common date is YYYY-MM-DD, and for time is hh:mm:ss. Either format can be understood, unambiguously, worldwide.

    See also: http://jibbering.com/faq/#dates

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