Why is the value output by MomentJS not reflecting UTC time? - javascript

I am trying to get a particular day/time in UTC format. Example code
const today = moment().tz('America/New_York')
const tomorrow = today.add(1, 'days')
const formatTime = today.format('YYYY-MM-DD')
const time = '7:00 pm'
const deliveryTime = moment(`${formatTime} ${time}`)
console.log(deliveryTime.utc().format())
Shouldn't I be getting this as output 2019-10-01T19:00:00Z as I am getting it on https://repl.it/#azs06/SilkyBronzeInversion
Suppose to 2019-10-02T13:00:00Z this on https://jsfiddle.net/azs06/hkrvze1x/2/
Is this is because of Node.js implementation of date?
What would be proper way to convert date time to UTC using moment.js?

Related

Use date-fns to format day as UTC

I have the following date format 2022-07-20 and I would like to parse into the following format 2022-07-20T00:00:00.000Z
I'm using the following code
const main = () => {
const day = '2022-07-20'
const date = new Date()
const result = parse(day.toString(), 'yyyy-MM-dd', date)
console.log(result.toISOString())
}
And getting the following output 2022-07-19T21:00:00.000Z. I assume this happening because my timezone is UTC+3.
How do I get it formatted as 2022-07-20T00:00:00.000Z?

Days between dates in DayJS UK Format

Im using dayjs, and I'm trying to calculate the days between 2 days so that I can output Today, Tomorrow and X days time
The problem I'm having though seems to be with the date formats
const date1 = dayjs('31/10/2021');
const date2 = dayjs().format("DD/MM/YYYY")
let hours = date2.diff(date1, 'days')
No matter how I try, I'm getting date2.diff is not a function when I add .format to date2. Does anyone know how to format this correctly and get the correct outcome?
If i try const date1 = dayjs('31/10/2021').format("DD/MM/YYYY"); then it errors with invalid date
You can try the following:
const date1 = dayjs('2021-01-25');
const date2 = '01/11/2020'; // or '2020-11-01'
let hours = date1.diff(date2, 'day')
console.log(hours) // 380
The format function just returns a string. This is why date2.diff is not a function.
The diff function is only available for dayjs objects (not for strings).
Docs for difference function: https://day.js.org/docs/en/display/difference
Docs for format function: https://day.js.org/docs/en/display/format
const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'day', true)
You don't need to call format function, just use dayjs constructor to convert both dates to dayjs object and return the difference between the 2 dates in number of days.

How to create a Date object for a given timezone and then convert it to UTC

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!
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'
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.

Moment JS days difference issue

I'm calculating the days difference between two dates using moment.js, but seems it wasn't calculated correctly.
const moment1 = moment(new Date('2020-04-05'));
const moment2 = moment(new Date('2020-01-06'));
const diff = moment1.diff(moment2 , 'days');
console.log(diff);
The output is 89 whereas the actual value should be 90. Specifically, if I change moment1 to '2020-04-04', the output is still 89. But after '2020-04-05' the value is always 1 day less than the actual value. Why?
You can simply pass dates you are checking for difference instead of using new Date() There is not requirement to use new Date() at all
Simple pass the date as string to moment and you will have 90 days.
Here is what moment.js diff says.
Run snippet below.
const moment1 = moment('2020-04-05');
const moment2 = moment('2020-01-06');
const diff = moment1.diff(moment2 , 'days')
console.log(diff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

Get timestamp from date string without timezone and timezone name apart

I have a dateTime string in this (bad, I know) format coming from an external API:
const startDate = '2/13/2020 15:00';
and the timezone name:
const timezoneName = 'America/New_York';
Which means that the dateTime is 2/13/2020 15:00 in New York.
Any idea for an elegant way to get timestamp (or JavaScript date object)?
I don't mind using moment.js or/and moment-timezone if it helps.
Moment and Moment-Timezone are for legacy code.
For new applications, the Moment team recommends Luxon.
const startDate = '2/13/2020 15:00';
const timezoneName = 'America/New_York';
const dateTime = luxon.DateTime.fromFormat(startDate, 'M/d/yyyy HH:mm',
{ zone: timezoneName });
const utcDateTime = dateTime.toUTC();
const s = utcDateTime.toISO();
console.log(s); //=> "2020-02-13T20:00:00.000Z"
Using moment-timezone should work:
const moment = require('moment-timezone');
const t = moment.tz("2/13/2020 15:00", "MM/DD/YYYY HH:mm","America/New_York");
console.log(t.toISOString()); // Prints '2020-02-13T20:00:00.000Z'

Categories

Resources