How can I convert datetime microformat to local time in javascript?

前端 未结 2 1552
余生分开走
余生分开走 2021-01-16 15:54

I have a page that is currently using the datetime microformat to display a timestamp, but I have only been showing the human-readable time for my own time zone:

<         


        
相关标签:
2条回答
  • 2021-01-16 16:24

    EcmaScript formalized the addition of an ISO-8601 style string as an imput for a JavaScript date. Since most JS implementations don't support this, I created a wrapper to the Date object, that has this functionality. If you set the title tags to output in UTC/GMT/Z/Zulu offset, you can use my EcmaScript 5 extensions for JS's Date object.

    For DateTime values that are to be used in client-side scripts, I generally try to always do the following. Store date+time in UTC zone (even in databases). Transmit date-times in UTC zone. From client to server, you can use the .toISOString() method in the above link. From server-to client this is relatively easy.

    Via jQuery (with extension):

    $('.published').each(function(){
      var dtm = new Date(this.title);
      if (!isNaN(dtm)) {
        this.text(dtm.toString());
      }
    });

    I don't recall if I added support for non-utc date-times in the input, but wouldn't be too hard to account for them.

    0 讨论(0)
  • 2021-01-16 16:38

    Here is code of mine that parses an ISO timestamp:

    function isoDateStringToDate (datestr) {
      if (! this.re) {
        // The date in YYYY-MM-DD or YYYYMMDD format
        var datere = "(\\d{4})-?(\\d{2})-?(\\d{2})";
        // The time in HH:MM:SS[.uuuu] or HHMMSS[.uuuu] format
        var timere = "(\\d{2}):?(\\d{2}):?(\\d{2}(?:\\.\\d+)?)";
        // The timezone as Z or in +HH[:MM] or -HH[:MM] format
        var tzre = "(Z|(?:\\+|-)\\d{2}(?:\\:\\d{2})?)?";
        this.re = new RegExp("^" + datere + "[ T]" + timere + tzre + "$");
      }
    
      var matches = this.re.exec(datestr);
      if (! matches)
        return null;
    
      var year = matches[1];
      var month = matches[2] - 1;
      var day = matches[3];
      var hour = matches[4];
      var minute = matches[5];
      var second = Math.floor(matches[6]);
      var ms = matches[6] - second;
      var tz = matches[7];
      var ms = 0;
      var offset = 0;
    
      if (tz && tz != "Z") {
        var tzmatches = tz.match(/^(\+|-)(\d{2})(\:(\d{2}))$/);
        if (tzmatches) {
          offset = Number(tzmatches[2]) * 60 + Number(tzmatches[4]);
          if (tzmatches[1] == "-")
            offset = -offset;
        }
      }
    
      offset *= 60 * 1000;
      var dateval = Date.UTC(year, month, day, hour, minute, second, ms) - offset;
    
      return new Date(dateval);
    }
    

    Unfortunately, it doesn't handle timezone abbreviations either. You would have to modify the "tzre" expression to accept letters, and the only solution I know of to deal with timezone abbreviations in Javascript is to have a look-up table which you keep up to date manually in the event of changes to regional daylight savings times.

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