Moment & Timezone convert local time fails - node.js

This is in an Ionic based hybrid app on NodeJS.
Trying to convert a local time as specified by a user input to another timezone, yet it fails:
static MTL_local_time_to_server(aDateTime:moment.Moment):moment.Moment{
console.log(aDateTime.format('MMMM Do YYYY, h:mm:ss a'));
const localTime:moment.Moment = momenttz.tz(aDateTime, momenttz.tz.guess());
console.log(localTime.format('MMMM Do YYYY, h:mm:ss a'), momenttz.tz.guess());
const returnTime:moment.Moment = momenttz(localTime).tz("Europe/Berlin");
console.log(returnTime.format('MMMM Do YYYY, h:mm:ss a'));
return returnTime;
}
Prints
April 22nd 2019, 12:00:00 am
April 22nd 2019, 12:00:00 am America/Los_Angeles
April 22nd 2019, 9:00:00 am

A few things:
You've typed the output of moment.tz as a string, but it's actually a Moment object.
When you call JSON.stringify on a Moment object, it returns the output of .toISOString(), which is always in UTC. (The Z indicates UTC in the ISO 8601 format.)
It's not clear if your input is a string or a Date object. If it's a string, then the Z indicates UTC, so it will always be interpreted as UTC. If it's a Date object, then the point in time represented by that Date object will be used - which depends on how you constructed the Date object. Either way, the value you're showing is UTC based, not local time.
It's not clear exactly what you're trying to accomplish. From your variable names, it would seem like you are trying to convert localDateTime to localTime, which logically would give the same value if they were both "local".
If you're trying to convert a value from local time to Berlin time, then:
moment(yourInput).tz('Europe/Berlin')
If you're trying to convert a value from Los Angeles time to Berlin time, then:
moment.tz(yourInput, 'America/Los_Angeles').tz('Europe/Berlin')
If you're trying to convert a value from UTC to local time, you don't need moment-timezone at all:
moment.utc(yourInput).local()
If you need string outputs, then you should call the format function to produce a string. What you showed here looks like you are logging Moment objects not strings.

Related

Reading date in excel file through ExcelJS give wrong date

I am working at EST timezone. When I reading a date in Excel file with the help of ExcelJS. The date in Excel is 3/31/2021. But the same date is retrieving as Tue Mar 30 2021 20:00:00 GMT-0400 (Eastern Daylight Time). So it is considering the given date as UTC and converting it to my current timezone. I dont want to convert it to UTC and then to my current timezone. I want to read the direct date which is present in the EXCEL file.
I see dateUTC: false somewhere in the document. This is used at Writing the file. But how to do it when I am reading the excel file. Any help on this?
First I converted the GMT date to milliseconds using Date.parse() and added the time difference between GMT and the current time zone (in my case Asia / Tashkent +5) to the resulting value, then converted it back to a regular date using new Date(milliseconds)
let m = Date.parse(gmtDate); //GMT date in milliseconds
let mTZ = m + 18000000; //Asia/Tashkent +5 time zone. 18.000.000 = 5 hours in milliseconds.
let currentDate = new Date(mTZ);
I ran into this recently and had to do the same thing:
Read from excel as UTC
then convert into the timezone I wanted for data
I know from your question you don't want to do this, so perhaps this plan is something to fall back on.
My code was something like this:
const moment = require('moment-timezone')
let utc = moment.utc(excelCellStr, 'M/D/YY hh:mm A')
let t = moment().tz('America/New_York')
t.year(utc.year())
t.month(utc.month())
t.date(utc.date())
t.hour(utc.hour())
t.minute(utc.minute())
t.second(utc.second())
excelCellStr = t.format()
I am not sure if there is a setting to avoid this or not. We are using exceljs as well.

Freemarker ISO string to datetime with timezone

I need to display the below "String" in the desired format
String str = 1979-01-24T00:00:00.000-08:00
Desired format: Jan 24, 1979 00:00:00 AM PST
Note: The tz in the str could be any tz not limited to PST.
Tried the below but none worked:
str?datetime.iso - Output is Jan 24, 1979 2:00:00 AM CST - This displays the date time in the format I need but the time is being converted from PST to CST.
str?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Expected a method, but this has evaluated to a string
str?datetime?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Unparseable date: "1979-01-24T00:00:00.000-08:00"
<#setting datetime_format="iso"> str?datetime - 1979-01-24T02:00:00-06:00 - The timezone is changed.
The problem here is that FreeMarker parses date/time values to java.util.Date (and its subclasses), which don't store a time zone anymore, as it always stores the value in UTC. So that information is lost after parsing. As of 2.3.30, the only solution I see to do this in Java (with Java 8 ZonedDateTime).
The timezone can be configured by the following setting, as refer to their documentation https://freemarker.apache.org/docs/ref_directive_setting.html
<#setting time_zone ="PST">
<#assign str = "1979-01-24T00:00:00.000-08:00">
${str?datetime.iso}

Possible to specify UTC+3 to linux date when creating a timestamp string?

I am trying to create a timestamp string:
TS=$(date -d "today" +"%Y_%d_%m_%H%M%S")
echo "TS = $TS"
But I need it to be in UTC+3. The man pages on date does not show that as an option and I don't want to modify the OS locale.
I have tried:
$ date -d "today" +"%Y_%d_%m_%H%M%S +0300"
2020_16_04_090342 +0300
$ date -d "today" +"%Y_%d_%m_%H%M%S +0400"
2020_16_04_090347 +0400
So seems it has no effect. Also the string should NOT contain the offset, should just be:
2020_16_04_120347
Any suggestions?
man date
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.
date -d "today" +"%Y_%d_%m_%H%M%S +0300"

converting a twitter date string into a datetime field in excel

I have a number of excel strings in the format "Mon Nov 25 17:20:47 +0000 2019"
I found an earlier post that recommended using =DATE(RIGHT(O2,4),MONTH(DATEVALUE(1 & MID(O2,5,3))),MID(O2,9,2)) to create a usable date field. However, this drops the time which is an important piece of information.
How can I include the time with the date in order for excel to recognize and sort all the information included in the field?
Thank you in advance!
You can just use the same logic with the date formula, but use TIME instead of DATE and of course extract the correct time into the formula =TIME(MID(O2,12,2),MID(O2,15,2),MID(O2,18,2))
Edit:
to combine them both in one field, you will need to add them =DATE(RIGHT(O2,4),MONTH(DATEVALUE(1 & MID(O2,5,3))),MID(O2,9,2)) + TIME(MID(O2,12,2),MID(O2,15,2),MID(O2,18,2))
The rationale of this is because:
Date is expressed in whole numbers i.e. 1 = 01/01/1900, 2 = 02/01/1900, 3 = 03/01/1900... 43794 = 25/11/2019, etc.
Time is expressed as a fraction of the day i.e. 0.5 = 12 hrs/12PM, 0.66666 = 16 hrs/4PM, etc.
so lets say you have 1/1/2019 12.00 PM, the date part that gives 1/1/2019 will be 43466 and the time part will be 0.5. Adding them together will give you 43466.5, and when converted to a date time format it will show as 1/1/2019 12:00 PM.
You can use string functions to create an unambiguous date string, then turn it into a date/time value with a mathematical operation (adding the time value in a string form).
=(MID(A1,9,2)&"-"&MID(A1,5,3)&"-"&RIGHT(A1,4))+MID(A1,12,8)
You'll need to format the result as something appropriate: eg: dd-mmm-yyyy hh:mm

How to extract time from datetime field and modify only time to 9:00 PM using groovy?

I am trying to copy values across 2 datetime fields. While copying I want to set the time to 9:00 PM and pass the date value as it is.
Can anyone help on how to do this
I'm not sure I understand your question, but if you want to set the time component of a java.util.Date to 9PM, this should do it
Date date = new Date()
date.clearTime()
date.set((Calendar.HOUR_OF_DAY): 21)
However, this modifies the source Date object in-place. To avoid this, use the following instead:
Date date = new Date()
Date dateAt9PM = new Date(date.getTime()).clearTime()
dateAt9PM.set((Calendar.HOUR_OF_DAY): 21)
You can also use some groovy magic (C)
Date orig = new Date() + 10
Datew newDate = orig.updated( hourOfDay:19, minute:42, second:33 )
gives
Sat Mar 23 19:42:33 UTC 2019

Resources