How do you work on such a format?


  • QA Engineer

    Party service shall give a date in such a format:

    /Date(1332446400000+0300)/
    

    Like, JavaScript Can you process it properly?
    I see that's possible. moment.js to the facility Dat(*) and return what is needed, however:

    typeof /Date(1332446400000+0300)/ == "object" // true
    

    How am I supposed to get rid of the redneck so I can put it in peace. moment Is that a format?
    I have to pre-test the variables for Dat's membership.

    /Date(1332446400000+0300)/ instanceof Date; // false
    


  • Such a format Date.parse It doesn't bite, because it's incorrect.
    It's better to cut milliseconds, put it as a number in the designer. Date And maybe play with the shift. UTC
    Truth with the last one JS Problems, we could try some crutches.

    let str = '/Date(1332446400000+0300)/'; // Ваша строка
    // Выдираем длинное число миллисекунд и UTC
    let [, time, utc] = /Date\((\d+)((?:\+|-)0\d00)/.exec(str);
    

    // Валидный объект Date
    // Сначала создаём объект через мс
    // Потом возвращаем GTM представление и подменяем на нужный пояс, получается что-то типа:
    // Thu, 22 Mar 2012 20:00:00 GMT+0300
    // Так Date.parse поймёт и переведёт в нужный пояс
    console.info(new Date(new Date(+time).toGMTString().replace('GMT', GMT${utc})));


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2