NodeJs How to use moment-timezone to convert Epoch to Specific TImezone and format - node.js

I have a use-case in which I want to convert an Epoch integer value into a specific Timezone time and a specific format,
Also, I want to convert a human-readable date time into epoch.
I am trying to use moment-tz for the timezone conversion.
I using a specific Epoch timestamp 1555296000 which is -
Monday, April 15, 2019 10:40:00 AM in Kuala Lampur Malaysia,
I am able to convert 2019-04-15 10:40:00 of Asia/Kuala_Lumpur timezone into correct Unix.
But I am unable to convert 1555296000 into another timezone's unix,
i.e I wish to convert 1555296000 into equivalent YYYY-MM-DD hh:mm:ss of Asia/Calcutta timezone.
Following is the code I'm trying to work with -
var moment = require('moment-timezone');
console.log("Convert from Asia/Kuala_Lumpur to Unix -> ", moment.tz("2019-04-15 10:40:00","Asia/Kuala_Lumpur").unix());
// Outputs - 1555296000
console.log("Epoch to Specific TimeZone and Format -> ",moment(1555296000).tz("Asia/Calcutta").format('YYYY-MM-DD hh:mm:ss'));
// Outputs - 1970-01-19 05:31:36
// I want - 2019-04-15 08:10:00

Try this out
const moment = require("moment-timezone");
console.log(
moment
.unix(1555296000)
.tz("Asia/Calcutta")
.format("YYYY-MM-DD HH:mm:ss")
);
2019-04-15 08:10:00 - "Asia/Calcutta" is 2019-04-15 10:40:00 - "Asia/Kuala_Lumpur"

Related

What is the correct way to parse time in a timezone using momentjs

I am trying to parse time using following code line
const fromAnchorDate = moment.tz('09:00 AM', "hh:mm A", "America/New_York").tz("America/New_York");
However, when I look at this object why does it show time as 1:00 AM in PST. Shouldn't it be 6 AM PST.

How to determine the appropriate the timezone to apply for historical dates in a give region in python3

I'm using python3 on Ubuntu 20.04.
I have a trove of files with naive datetime strings in them, dating back more than 20 years. I know that all of these datetimes are in the Pacific Timezone. I would like to convert them all to UTC datetimes.
However, whether they are relative to PDT or PST is a bigger question. Since when PDT/PST changes has changed over the last 20 years, it's not just a matter of doing a simple date/month threshold to figure out whether to apply the pdt or pst timezone. Is there an elegant way to make this determination and apply it?
Note upfront, for Python 3.9+: use zoneinfo from the standard library, no need anymore for a third party library. Example.
Here's what you can to do set the timezone and convert to UTC. dateutil will take DST changes from the IANA database.
from datetime import datetime
import dateutil
datestrings = ['1991-04-06T00:00:00', # PST
'1991-04-07T04:00:00', # PDT
'1999-10-30T00:00:00', # PDT
'1999-10-31T02:01:00', # PST
'2012-03-11T00:00:00', # PST
'2012-03-11T02:00:00'] # PDT
# to naive datetime objects
dateobj = [datetime.fromisoformat(s) for s in datestrings]
# set timezone:
tz_pacific = dateutil.tz.gettz('US/Pacific')
dtaware = [d.replace(tzinfo=tz_pacific) for d in dateobj]
# with pytz use localize() instead of replace
# check if has DST:
# for d in dtaware: print(d.dst())
# 0:00:00
# 1:00:00
# 1:00:00
# 0:00:00
# 0:00:00
# 1:00:00
# convert to UTC:
dtutc = [d.astimezone(dateutil.tz.UTC) for d in dtaware]
# check output
# for d in dtutc: print(d.isoformat())
# 1991-04-06T08:00:00+00:00
# 1991-04-07T11:00:00+00:00
# 1999-10-30T07:00:00+00:00
# 1999-10-31T10:01:00+00:00
# 2012-03-11T08:00:00+00:00
# 2012-03-11T09:00:00+00:00
Now if you'd like to be absolutely sure that DST (PDT vs. PST) is set correctly, you'd have to setup test cases and verify against IANA I guess...

convert string to ISO date time in nodejs

Convert this date into ISO format in nodejs
created_at="September 17th 2019, 16:50:17.000";
let new_time = new Date(created_at);
created_at = new_time.toISOString();
console.log(created_at);
Output: Invalid Date
Exacting output is in ISO format. like this 2011-10-05T14:48:00.000Z
Moment.js is a library which you can use to get the output and do some advanced operations with date and timezones.
Below is the code to get expected output.
var moment = require('moment')
created_at="September 17th 2019, 16:50:17.000";
let new_time = moment("September 17th 2019, 16:50:17.000", "MMMM Do YYYY, HH:mm:ss:SSS");
created_at = new_time.toISOString();
console.log(created_at);
You will have to pass the date string in below format in order to convert it to ISO date:
var date1 = "September 17 2019, 16:50:17.000";
let new_date = new Date(date1);
console.log(new_date);
console.log(new_date.toISOString());

Format the time from moment js

When I am trying to receive mail from gmail, I get time in this format (Mon, 12 Jun 2017 10:29:07 +0530). I want to calculate time minus current time. How to do so?
var testDate = moment("Mon, 12 Jun 2017 10:29:07 +0530");
//Relative to time in human readble format
testDate.fromNow(); //3 days ago
You can simply call the moment constructor with the given Date format. moment.js is smart enough to parse it for you. To get the difference you can convert it into unix based time format and subtract it.
const givenTime = moment("Mon, 12 Jun 2017 10:29:07 +0530").unix()
const currentTime = moment().unix()
//Difference in milliseconds
const diff = givenTime - currentTime

Convert yyyymmddThhmmss.SSSZ format to Unix Timestmp

I'm using NodeJS to fetch a date from a server, but the date format is yyyymmddThhmmss.SSSZ
20170423T203146.000Z
I'm trying to convert this date string into epoch time, to easily calculate a difference between this time and the current time. (the timezone will always be UTC)
However, I could not find any possibility to parse this string, since libraries don't seem to accept this kind of date string.
Can someone help me out with this?
Moment.js seems to give me correct parsed date
var moment = require('moment')
var date = moment("20170423T203146.052Z" , "YYYYMMDDThhmmss.SSS")
console.log(date.format("YYYY MM DD hh mm ss SSS"))
Output: 2017 04 23 08 31 46 052
you should take a look at Moment.js String+Format + Special Format
It seems to work very well on your time-string already:
const moment = require("moment");
const werner = "20170423T203146.000Z";
console.log(moment.utc(werner).format());
const epochSec = moment.utc(werner).unix();
const epochMs = moment.utc(werner).valueOf();
console.log(epochSec, epochMs);
Viel Erfolg ;)

Resources