0

I wanted to convert a timestamp which i am getting as below format

"2019-08-31T00:00:00+0800"

when i am converting into date using javascript it is giving me

Fri, 30 Aug 2019 16:00:00 GMT

But the desired date is

31 Aug 2019

I have noticed that there is +0800 at the end of the timestamp, if i add that time to the GMT time, it is giving me the desired result. Is there any method or a date function to convert it. or do we have any angular pipes to convert it into the desired date ?
which format of date is it actually ?

can refer to this link for GMT +8 offset - https://greenwichmeantime.com/time-zone/gmt-plus-8/

this is yash
  • 533
  • 1
  • 3
  • 15

3 Answers3

0

For dates and time it is easier to work with Moment, so you can use the format function:

Moment Format

Or use the toLocaleDateString function:

toLocaleDateString Prototype

So you can do this:

var options = {year: 'numeric', month: 'long', day: 'numeric' };
console.log(event.toLocaleDateString('en-EN', options));

Will give you this:

"December 20, 2012"

You can play with the options to get the wanted result.

0

Or you could use date-fns which is more modular. https://date-fns.org/. You can import only the parts of date-fns that you use. Moment is big

Jens Alenius
  • 1,931
  • 2
  • 16
  • 20
0

You could try something like this. It is no function, it's a crude way. You can check it out if it works for you. Try with all the different cases to verify.

var this_date = '2019-08-31T00:00:00+0800'
var date = new Date(this_date).toDateString()
var date_list = date.split(' ');
date = [ date_list[2], date_list[1], date_list[3] ].join(' ')
console.log(typeof date);
console.log(date)

var this_date = '2019-08-31T00:00:00+0800'
var date = new Date(this_date)
console.log(date.toString())

You can as well take a look here, an older stackoverflow post. You can use it as a function if you like it. If you are looking for something else, Please give the functions you are using for conversion and as well try to find the 'typeof' variable.

naman1994
  • 315
  • 5
  • 11