-1

I have the following strings on my NodeJS server, which is uses UTC time.

const year = '2021';
const month = '05';
const day = '28';
const hour = '17';
const minute = '40';
const second = '00';

If I now create a Date object like so new Date(year, month, day, hour, minute, second) it obviously creates the Date object for UTC. However, the strings I'm parsing are were recorded in CET and not UTC.

How can I parse the information, like shown above, as Date for a given timezone on a server that is set to UTC? I also need to respect summer- and wintertime for the respective time zones.

Afterwards, I actually need to convert that Date object to UTC as I need it as UTC timestamp to send it to an external system.

I have quite a hard time wrapping my head around it. Thanks!

jz22
  • 2,328
  • 5
  • 31
  • 50
  • Check https://momentjs.com/ – Arvind Kumar Avinash May 27 '21 at 19:22
  • Does moment actually respect summer- and wintertime, if a timezone has it? – jz22 May 27 '21 at 19:23
  • Please check this, https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone – Vivek Bani May 27 '21 at 19:39
  • "*obviously creates the Date object for UTC*". No it doesn't. It makes a Date based on the host system offset and rules, it's represented as an offset from 1970-01-01T00:00:00 UTC. – RobG May 27 '21 at 21:53
  • 1
    @ArvindKumarAvinash—the [moment project](https://momentjs.com/docs/#/-project-status/) is in maintenance mode and has essentially been deprecated. [Recommended alternatives are here](https://momentjs.com/docs/#/-project-status/recommendations/). Both [Luxon](https://moment.github.io/luxon/) and [date-fns](https://date-fns.org/) will do what the OP wants (and perhaps others too). – RobG May 28 '21 at 02:29

2 Answers2

-2

Try this...

var date = new Date(year, month, day, hour, minute, second); 
var utc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(),
 date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

console.log(new Date(utc));

Look at moment.js --> https://momentjs.com/timezone/docs/ You might have to play around with the conversion, but this shouldn't be too difficult.

RickyN8
  • 17
  • 4
  • Since the server is set to UTC the `date` will already be UTC. – jz22 May 27 '21 at 19:38
  • 1
    The first part does exactly what the OP says they don't want to do, the second part is redundant and can be `let utc = date.getTime()` or just `let utc = +date`. – RobG May 27 '21 at 21:56
-2

If u Are using Nodejs then install this package date-and-time

using this cammand

npm i date-and-time

then require it

const date = require('date-and-time');

your date

const now = new Date();
date.format(now, 'YYYY/MM/DD HH:mm:ss');    // => '2015/01/02 23:14:05'
date.format(now, 'ddd, MMM DD YYYY');       // => 'Fri, Jan 02 2015'
date.format(now, 'hh:mm A [GMT]Z');         // => '11:14 PM GMT-0800'
date.format(now, 'hh:mm A [GMT]Z', true);   // => '07:14 AM GMT+0000'
Fida Khan
  • 19
  • 4
  • 1
    The OP wants to parse a timestamp with offset then produce a time value. This answer doesn't do either. – RobG May 27 '21 at 21:54
  • If u visited that date and time package page there u can see that , just like format there is a PARSE function read it u will understand – Fida Khan May 28 '21 at 04:50
  • You've completely missed the point. The OP doesn't want to parse anything, they want to create a Date for a particular set of values for year, month, day, etc. in a particular timezone. The formatting part is trivial. – RobG May 28 '21 at 06:25