0

I am pretty new to Javascript, I have a requirement where I'll get a Date as one of the input value in format YYYY-MM-DD (Eg: 2014-07-01), and I need to convert this to UTC offset (-4 or -5) based on daylight saving time (Eg: 2014-07-01T00:00:00-04:00". I am using below piece of code, but it's not adding the offset value at the end of the date string instead I am getting like this "2014-07-01T00:00:00.000Z".

            if (wheelRequest.RegEndDate){
                var regEndDate = new Date(wheelRequest.RegEndDate);
                var regEndDateFormatted = new Date(regEndDate).toISOString();
                licensenumObj.enddate    =    regEndDateFormatted ;
            }

I've verified the other post (How to format a JavaScript date) but it doesn't mention how to show the offset value at the end. Please advise on how I can able to add the offset based on my need.

Thanks, Suman.

Suman R
  • 1
  • 1
  • Some ideas [here](https://usefulangle.com/post/30/javascript-get-date-time-with-offset-hours-minutes). FYI `regEndDate` is already a `Date` object. No need for `new Date(regEndDate)`. – jarmod Nov 12 '20 at 00:00
  • Note that dates in the format YYYY-MM-DD are parsed as UTC, If you simply want to add a time of 00:00:00 with the host timezone offset, then convert the local offset (returned in minutes by [*getTImezoneOffset*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset)) to HH:mm format, taking care to reverse the sign, and append the time and offset to the string. – RobG Nov 12 '20 at 02:43
  • You can also add the offset using the *timeZone* option with [*Intl.DateTimeFormat.formatToParts*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts) to get the offset as explained in [this answer](https://stackoverflow.com/a/3552493/257182). – RobG Nov 12 '20 at 22:18

2 Answers2

0

if you want to work with moment-timezone, where to use it

const moment = require('moment-timezone');

const input = '2020-11-11';

const time = moment(input);
time.format();

output: Wed Nov 11 2020 00:00:00 GMT-0300

changing timezone

const time = moment.tz(input, 'Asia/Tokyo');
time.format();

output: Wed Nov 11 2020 12:00:00 GMT+0900

patrickfv
  • 26
  • 2
  • Hi Patrick, here my requirement is to get the date from "2014-07-01" to like this "2014-07-01T00:00:00-04:00" , where the offset will be at the end. please advise. – Suman R Nov 12 '20 at 02:43
-1

I'm thinking you need to see this:

function specialTime(dateString){
  let dt = new Date(dateString), os = dt.getTimezoneOffset()/60;
  if(os < 0 && os > -10){
    os = '+0'+Math.abs(os);
  }
  else if(os < 10){
    os = '-0'+os;
  }
  else{
    os = '-'+os;
  }
  return dt.toISOString().replace(/\..+$/, os+':00');
}
console.log(specialTime('2014-07-01'));
StackSlave
  • 10,198
  • 2
  • 15
  • 30
  • Hi StackSlave, when running this I am still getting similar to what I am currently ("2014-07-01T00:00:00.000Z"), here my need was to get the date like this "2014-07-01T00:00:00-04:00" where the offset will be at the end. Any tweaks to get like this way?, please assist. – Suman R Nov 12 '20 at 02:41
  • 1
    Looks like this should work for me @StackSlave, thanks much, will try to incorporate in my code. – Suman R Nov 12 '20 at 13:34