0

So this is a new one to me. I've been working with this api and they returned a date in json format that looks like this

{
 DateAdd: "/Date(1582936941390-0600)/"
}

not exactly sure how to convert this to a datetime like in the format below so I can actually do something with it.

2020-03-13 23:08:00

i have never seen this date format before! Thanks

Markese P
  • 105
  • 1
  • 10
  • It's probably some sort of epoch time format e.g. number of seconds from 1970 or something. Recommend importing momentjs into your project and using that to make your life easier. – marblewraith Mar 11 '20 at 04:20
  • Just FYI, moment.js is a 329 kb library. – Cully Mar 11 '20 at 04:23
  • Does the API have documentation concerning its date/timestamp formats? – Cully Mar 11 '20 at 04:25
  • No it doesnt provided any documentation. But I will look into moment JS seems like everyone is using this – Markese P Mar 11 '20 at 04:26
  • You could potentially use moment.js to figure out what the format is. Maybe it has some kind of format identification features. Once you figure out what the format is, you could write your own function to parse it. But if you're using this on the frontend, you absolutely shouldn't use moment.js; it's gigantic. [date-fns](https://date-fns.org/) is a great alternative. Though I don't know definitively if it would handle this format. – Cully Mar 11 '20 at 04:30
  • Sounds good dude thanks for all the support , most definitely opened me up to a new library that I can tell I will enjoy :) – Markese P Mar 11 '20 at 04:38
  • FYI - This was the format that ASP.Net used to use when serialising `DateTime`s – phuzi Mar 11 '20 at 13:00
  • See [* How do I format a Microsoft JSON date?*](https://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date) – RobG Mar 12 '20 at 00:14

3 Answers3

1

Use moment.js to convert the date format

var data = {
 DateAdd: "/Date(1582936941390-0600)/"
}
var datam = moment(data.DateAdd)
console.log(datam.format("YYYY-MM-DD HH:mm:ss")) // 2020-02-29 07:42:21
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
Kristian
  • 2,456
  • 8
  • 23
  • 23
  • 1
    Just FYI, moment.js is a 329 kb library. – Cully Mar 11 '20 at 04:22
  • So Krisitan thanks for answering! Seems simple dude. Thanks @Cully I was just going to ask should I use a whole library for this 1 thing ? Quite small – Markese P Mar 11 '20 at 04:24
  • Note that moment.js **ignores** the timezone offset. This is equivalent to [Robo Robok's answer](https://stackoverflow.com/a/60629268/257182) with formatting. – RobG Mar 12 '20 at 00:58
0

If you don't care about the timezone, you can just cut the timestamp out of that string:

const dateString = data.DateAdd;
const date = new Date(Number(dateString.match(/\d+/)[0]));
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
0

You can convert date into desired format by Javascript date object.

let addZero = (i) => {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }
    
let formatDate = (date) => {
        let year = date.getFullYear(),
            month = addZero(date.getMonth() + 1),
            day = addZero(date.getDate() + 1),
            hours = addZero(date.getHours() + 1),
            minutes = addZero(date.getMinutes() + 1),
            seconds = addZero(date.getSeconds() + 1);
        let dateArr = [year, month, day];
        let timeArr = [hours, minutes, seconds];
        let result = dateArr.join('-').concat(" ", timeArr.join(':'));
        return result;
    }
    
let inputString = "/Date(1582936941390-0600)/";
let inputData = new Date(Number(inputString.match(/\d+/)[0]));

console.log(formatDate(inputData));

Please read more about Javascript date object

Jeni
  • 320
  • 2
  • 12