0

There is no shortage of questions/answers on different variations of this, but I can't seem to find my scenario.

How (in NodeJS) do you convert strings like the following;

let timeZone = "America/Chicago";
let dateTimeStr = "01-26-2020 12:34:56 PM";

to a date time string with an offset like this? 2020-01-26T17:34:56-05:00

Here is what I'm trying

let timeZone = "America/Chicago";
let dateTimeString = "01-26-2020 12:34:56 PM";
newDate = new Date(dateTimeString);

console.log(`newDate=${newDate.toISOString()}`);

let dateTimeWithOffset = new Date(newDate).toLocaleString("en-US", {
  timeZone: timeZone,
  timeZoneName: "short",
});
console.log(`dateTimeWithOffset=${dateTimeWithOffset}`);

Which produces;

newDate=2020-01-26T17:34:56.000Z
dateTimeWithOffset=2020-01-26T17:34:56.000Z

I am looking to do this without any external lib.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
DBtake3
  • 106
  • 8
  • Note that parsing that `dateTimeString` using the Date constructor is not guaranteed to work across browsers or locales. You're likely better off using the answers to [Converting a string to a date in JavaScript](https://stackoverflow.com/q/5619202/215552) to do that. `toISOString` doesn't take any parameters; you're thinking of `toLocaleString`. – Heretic Monkey Nov 18 '20 at 16:44
  • Oh @HereticMonkey you're totally right on the toISOString vs toLocaleString ... I'll correct my code above. I probably should have mentioned that I'm looking to do this in node in AWS Lambda, so no concerns of Browser compatibility. I will check out the answer you mentioned. – DBtake3 Nov 18 '20 at 18:01
  • No luck with the possible answer linked in. The part I'm really struggling with is the offset. – DBtake3 Nov 18 '20 at 18:19

1 Answers1

1

I know that you said you don't want to use any external libs, but from my experience, it's pretty hard to deal with different timezones, especially if you want to pass them trough an API or something. If you change your mind, you can use Moment JS for easily format and model dates from strings or even Date objects

// Parse timestamps using offset for provided location
var a = moment.tz("2013-11-18 11:55", "America/Toronto");
var b = moment.tz("May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");
var c = moment.tz(1403454068850, "America/Toronto");

console.log(
  a.format() + '\n'+ // 2013-11-18T11:55:00-05:00
  b.format() + '\n'+ // 2014-05-12T20:00:00-04:00
  c.format()         // 2014-06-22T12:21:08-04:00
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>

You can change the format too

moment().format();                                // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
moment().format("ddd, hA");                       // "Sun, 3PM"
moment().format("[Today is] dddd");               // "Today is Sunday"
moment('gibberish').format('YYYY MM DD');         // "Invalid date"
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks! My main reason for not wanting to utilize an external lib is simply because I'm running this code in an AWS Lambda function, actually about 10 functions and I felt adding an external npm lib in to all 10, or creating as a shared layer for one lib was going to be more work then a pure JS solution ... but perhaps I should reconsider since your solution's outcome produces exactly what I need. – DBtake3 Nov 20 '20 at 12:34