convert string to datetime  in javascript

pamela rizk
pamela rizk used Ask the Experts™
on
dear All

i have a string in javascript as below :
11/04/2019 17:30:00 (that represents month/day/year   and time )
i need to  convert this string to a datetime with the below format:
2019-11-04T17:30:00+02:00

how to do that ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
a = new Date("11/04/2019 17:30:00");
a.toISOString();

Open in new window


Capture.JPG
Michel PlungjanIT Expert
Top Expert 2009

Commented:
You mean this?
Expected format: 2019-11-04T17:30:00+02:00 - but I expect +01:00 since it is not DST

https://jsfiddle.net/mplungjan/ot0xmh9s/

const pad = num => ("0" + num).slice(-2);

const getFormattedDate = d => {
  const tzOffsetMin = d.getTimezoneOffset(),
    offsetHours = Math.floor(Math.abs(tzOffsetMin / 60)),
    offsetMin = Math.abs(tzOffsetMin % 60);
  let sign = "";
  if (tzOffsetMin < 0) sign = "+"; // opposite of what we should think
  else if (tzOffsetMin > 0) sign = "-";

  // we could steal the time from the toString and replace / GMT/,""

  return `${d.getFullYear()}-${pad(d.getMonth()+1)}-${pad(d.getDate())}` +
    `T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}` +
    `${sign}${pad(offsetHours)}:${pad(offsetMin)}${sign===""?"Z":""}`;
}

Open in new window

pamela rizkDeveloper

Author

Commented:
Expected format: 2019-11-04T17:30:00+02:00
but what i am getting from your solution is
2019-11-04T16:30:00-01:00
why?
time is 16:30 instead of 17:30 why?please explain
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

pamela rizkDeveloper

Author

Commented:
yes better to have another solution for that
something more global and not specific
in all cases thank you
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Please see my updated answer.
I have written a function instead.
I still expect +01:00 and not +02:00 in Amsterdam in November
leakim971Multitechnician
Top Expert 2014

Commented:
something more global and not specific

That +02:00 is your original post is specific to YOUR timezone
What I post is the UTC + 00:00 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
You want +02:00 ?

a = new Date("11/04/2019 17:30:00");
a.setHours(a.getHours() + 2);
var d = a.toISOString().split(".")[0] + "+02:00"

Open in new window


Capture.JPG
Michel PlungjanIT Expert
Top Expert 2009

Commented:
That would fail across daylight saving
Developer
Commented:
i went to another turnaround where i send a string character ex:""04/11/019 17:30 " via xml and let the database manage it
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Hi Pamela

Great you found a solution you could use.

But you did not actually use the answer you accepted as solution?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial