Change ISO Date String to Date Object - JavaScript

前端 未结 8 1650
花落未央
花落未央 2021-01-01 08:54

I am stuck in a weird situation and unfortunately,even after doing some RnD and googling, i am unable to solve this problem.

I have a date string in ISO format, lik

相关标签:
8条回答
  • 2021-01-01 09:32

    For display purposes, you could simply do this... Given 'Date' in ISO format you could implement,

    <div> {{Date | date:"dd MMM yy hh:mm a" }} </div>
    

    A nifty trick to display (only display) the date in required format.

    Hope it helps!

    0 讨论(0)
  • 2021-01-01 09:33

    Do not pass strings to the Date constructor, it is notoriously bad at parsing strings. IE 8, for one, will not parse ISO 8601 format strings at all and return NaN. It's really simple to write your own parser:

    function parseISOString(s) {
      var b = s.split(/\D+/);
      return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6]));
    }
    

    Note also that if the time is 19:38:34.203 UTC and your timezone is UTC +0530, then the time in that timezone is 01:08:34 am on the following day, hence the difference in dates. For example, for a person on the east coast of Australia but not observing daylight saving (i.e. UTC +10), it's equivalent to:

    4 November, 2014 05:38:34
    

    Edit

    So if you want to return it to an ISO date, you can use the getISO* methods to create whatever format that suits, e.g.

    function isoFormatDMY(d) {  
      function pad(n) {return (n<10? '0' :  '') + n}
      return pad(d.getUTCDate()) + '/' + pad(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear();
    }
    
    var s = '2014-11-03T19:38:34.203Z';
    var date = parseISOString(s);
    
    console.log(isoFormatDMY(date)) // 03/11/2014
    

    or use ES5's toISOString:

     parseISOString('2014-11-03T19:38:34.203Z').toISOString(); // 2014-11-03T19:38:34.203Z
    

    A simple polyfill for pre ES5 browsers:

    if (!Date.prototype.toISOString) {
    
      Date.prototype.toISOString = function() {
    
        var d = this;
    
        // Padding functions 
        function pad(n) {return (n<10? '0' :  '') + n}
        function padd(n){return (n<100? '0' : '') + pad(n)}
    
        return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) +
               'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + 
               pad(d.getUTCSeconds()) + '.' + padd(d.getMilliseconds()) + 'Z';
      }
    }
    
    0 讨论(0)
  • 2021-01-01 09:34
    new Date(this.datePipe.transform(isoDate,'yyyy-MM-dd HH:mm:ss', 'UTC'));
    

    Angular datepipe transform will convert it to a simple date time format as specified with no timezone. In case of ISO string format, the date constructor would have treated it as UTC time, but now, the Date constructor treats it as the local time.

    Example in my case(IST):

    input string: 2020-04-19T09:15:00.000Z
    after transform this.datePipe.transform(input string)
    2020-04-19 09:15:00
    Date object 
    new Date(this.datePipe.transform(isoDate,'yyyy-MM-dd HH:mm:ss', 'UTC'));//treats the transformed date as local timezone date when creating the date object
    Sun Apr 19 2020 09:15:00 GMT+0530 (India Standard Time)
    
    0 讨论(0)
  • 2021-01-01 09:36

    I started of with kevinmicke's excellent answer, but since I needed this for a code generator, I was concerned with code size. Eventually I ended up with this:

    const parseDate = dateString => {
      const b = dateString.split(/\D+/);
      const offsetMult = dateString.indexOf('+') !== -1 ? -1 : 1;
      const hrOffset = offsetMult * (+b[7] || 0);
      const minOffset = offsetMult * (+b[8] || 0);  
      return new Date(Date.UTC(+b[0], +b[1] - 1, +b[2], +b[3] + hrOffset, +b[4] + minOffset, +b[5], +b[6] || 0));
    };

    0 讨论(0)
  • 2021-01-01 09:46

    Well it depends on what you want to do with the object later. You can always refer to "UTC" date functions of javascript.

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

    0 讨论(0)
  • 2021-01-01 09:48

    You can use "getUTCDate()" to get actual date.

    var d = new Date('2014-11-03T19:38:34.203Z');
    var n = d.getUTCDate();
    

    But it will return only date. to get month "getUTCMonth()" and to get year "getUTCFullYear()". Then construct all in to your format. For example

    var n=[];
    var d = new Date('2014-11-03T19:38:34.203Z');
    var s = d.getUTCDate();
    n.push(s);
    s=d.getUTCMonth();
    n.push(s);
    s=d.getUTCFullYear();
    n.push(s);
    

    Finally make n as an object.

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