Convert C# Generated Date to ISO format in Javascript

Asked

Viewed 264 times

2

Hello, I’m making a website where I use an API built in C#. My problem is that when I get a date from the server, it sends me in this format:

[{
    "dataLiberacao":"\/Date(928160400000-0300)\/",
    "dataReg":"\/Date(928160400000-0300)\/",
    "id":2147483647,
    "maquina":"Conteúdo da cadeia de caracteres",
    "status":true,
    "versao":"Conteúdo da cadeia de caracteres"
}]

I need to know how I can convert this into a common format for Javascript.

NOTE: I already tried with the API people to send me this string but unfortunately they will not change the project so...

  • 1

    You can use some jQuery library as momentjs ?

  • power can, but the less lib’s the better

1 answer

2


Download momentjs which is a library , that works with various date formats and various functions:

moment("/Date(1198908717056-0700)/").format('DD/MM/YYYY'); 

var data = moment("/Date(1198908717056-0700)/").format('DD/MM/YYYY'); 
console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment-with-locales.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

In your case it would be something:

moment(dataLiberacao).format('DD-MM-YYYY');

Another way:

With eval and new Date:

var data = eval('new Date(1198908717056-0700)');
console.log(data.toLocaleString());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

The two ways are functional, the momentjs is a small library does not disturb the loading of the page and as has been reported is very stable, but, also has another way that can solve your problem without a lib external.

References:

  • 2

    @Leandroluk, in the friend Virgilio answered with the lib I told you above, she is very stable and has been for years there =]

  • 1

    right, I honestly did not want to include another script on the page, I would rather create a "micro-function" for this than have countless others that I will not use but already solves my case rs

  • @Leandroluk you now have two options one with a 20k javascript mini-framework and the other with two lines and the command eval with new Date. See what’s best for you .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.