1

We have a small news site where each news has time and date of publication in the following format:

"2018-06-20T06:07:00.000+03:00"

We connected analytics to which we need to send time in the following format:

"Fri, 23 Feb 2018 15:00:00 +0200"

Tell me, please, how best to resolve this issue? We can refer this question to the server-side developer so that the request contains the date in the format we need, or we can do it on the front.

I still do not quite understand how to get the date corresponding to the date in the article, and then transform it into the desired format, but I think it is possible (and difficult)

Nico Diz
  • 1,484
  • 1
  • 7
  • 20
A.Burdonskaya
  • 511
  • 1
  • 4
  • 27
  • 2
    I don't fully understand-- is the question simply how to consume one date format and convert it to another? If so, I would highly recommend checking out [momentjs](http://momentjs.com/)-- date parsing can be a bit of a pain, and moment handles most of it for you. That said, it is a somewhat large library. – Alexander Nied Jun 09 '19 at 01:25
  • need to convert the finished date to a different format, not get a new one ... – A.Burdonskaya Jun 09 '19 at 02:08
  • It is definitely possible, and not difficult, if you use a library like the one @AlexanderNied recommended. It can take your date and format it however you like. – Herohtar Jun 09 '19 at 02:09
  • I do not see in the documentation of this library the ability to convert an existing date. Could you tell me, maybe I'm missing something? – A.Burdonskaya Jun 09 '19 at 02:11
  • 1
    If your date exists as a string like you show in your question, you will first have to [parse](http://momentjs.com/docs/#/parsing/) it, then you can use the resulting `moment` object to [format](http://momentjs.com/docs/#/displaying/format/) it. – Herohtar Jun 09 '19 at 02:13
  • thanks, that's exactly what I was looking for – A.Burdonskaya Jun 09 '19 at 02:18

2 Answers2

2

If they are using PHP you could try something like this:

$date = date_create_from_format("Y-m-d\TH:i:s.uP", "2018-06-20T06:07:00.000+03:00");
echo date_format($date,'D, d M Y H:i:s O');

That should give you a date like: Wed, 20 Jun 2018 06:07:00 +0300

That should work as long as the input date is always going to be of the same format.

SScotti
  • 2,158
  • 4
  • 23
  • 41
-1

As people suggested to me in the comments, the best solution using javascript is to use the library moment.js

The following code does the work I need.

moment("2018-06-20T06:07:00.000+03:00").format('ddd, D MMM YYYY HH:mm:ss ZZ');
A.Burdonskaya
  • 511
  • 1
  • 4
  • 27