0

So I am trying to make a post request to an API, and one of the values required is a date that according to there documentation should be in the following format

Start time of the timesheet, in ISO 8601 format 
(YYYY-MM-DDThh:mm:ss±hh:mm). Time should reflect the user's local time.

But when I try to make a new Date().toISOString() value in the ISO format I get this

2019-07-17T19:50:08.057Z

So I guess my question is, how can I produce the supposed format that they are looking for which is apparently a different ISO 8601 format? Or what would be the format for the following timestamp?

2018-07-25T13:10:23-07:00

here is the documentation to the api that I am playing around with https://tsheetsteam.github.io/api_docs/#create-timesheets

TJ Weems
  • 1,086
  • 3
  • 21
  • 37
  • Possible duplicate of https://stackoverflow.com/questions/10830357/javascript-toisostring-ignores-timezone-offset – IceMetalPunk Jul 17 '19 at 20:07
  • Likely the actual offset isn't relevant. Have you tried `toISOString().replace(/z/i, '+00:00')`? The sample strings are ISO 8601 compliant. – RobG Jul 17 '19 at 20:29
  • It does not like this part `.123Z` so if I do this ` new Date().toISOString().slice(0, -4).replace('.', '+00:00')` it works fine without having to install packages. Just replacing 'z' however still gives the same alarm. Thanks for the help! – TJ Weems Jul 17 '19 at 20:46

3 Answers3

0

Just remove the tail. Something like this.

console.log(new Date().toISOString().replace(/(.+)(\..+?$)/g,'$1'));
Alex Kudryashev
  • 9,120
  • 3
  • 27
  • 36
  • Thanks for the help @AlexKudryashev, however I think I still need the time zone offset at the end of the format. See Soleil suggestion above, I am about to try it out. – TJ Weems Jul 17 '19 at 20:19
  • That produces a UTC timestamp masquerading as local, so not a good idea. – RobG Jul 17 '19 at 20:21
0

You need set location time to make reference to meridian 0 + or - , you can set with library like momentjs, basically you set a reference to compare

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00
Soleil
  • 381
  • 4
  • 9
  • Your answer works however the package must be `moment-timezone` instead of just `moment`, atleast from what I found from trying it. Thanks for the help! Also note that I was able to get away with just removing the characters from the date string that the api did not like. See comments from original question. – TJ Weems Jul 17 '19 at 20:52
0

Your question is similar to Javascript date format like ISO but local but you want the timezone also, so:

function toISOLocal(date) {

  // Pad single digit numbers with leading zero
  function z(n){return (n<10?'0':'')+n}

  // Copy the input date
  var d = new Date(date);

  // Get offset and adjust
  var offset = d.getTimezoneOffset();
  d.setMinutes(d.getMinutes() - offset);

  // Build timestamp with adjusted date and local offset
  var sign = offset < 0? '+' : '-';
  offset = Math.abs(offset);
  var offsetStr = sign + z(offset/60|0) + ':' + z(offset%60);

  return d.toISOString().replace(/z$/i, offsetStr);
}

console.log(toISOLocal(new Date()));

However I suspect you can get by with the built–in toISOString and just replace the trailing Z with +00:00. You might need to remove the decimal seconds part also:

function modifyISO(d) {
  return d.toISOString().replace(/\.\d+/, '').replace(/z$/i,'+00:00');
}

console.log(modifyISO(new Date()));
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Yes the second method you suggest was the one that worked for my use case, It did not like the decimal or anything after it. The people who wrote the API I am working with have strict time stamp rules I guess, but thanks for the help! – TJ Weems Jul 17 '19 at 20:57
  • @TJWeems—great. :-) You can combine the replaces: `d.toISOString().replace(/\.\d+z$/i, '+00:00')`. – RobG Jul 18 '19 at 00:09