Reading date in excel file through ExcelJS give wrong date - node.js

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.

Related

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

Moment & Timezone convert local time fails

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.

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

How can I convert date to a readable form in Angular and also concatenate two dates?

For any event, Im getting dates in my html file in the following manner:
Start Date: 2017-09-25T10:00:00
End Date: 2017-09-25T11:00:00
In my Controller I have them saved as :
Event= {StartDate: start, EndDate: end}
I am looking for a way to output them in my html with some concatenation and trimming done to the date. My event date will always be the same so I would like to be displayed something like:
September 09 2017, 10 AM - 11:00 AM
Help Needed!
Here's a working plunker that will give you a feel for the date formatting you're after: https://plnkr.co/edit/tmxQY9RyqGzHwCKhvQN0?p=preview
The template interpolation is key here
{{Event.StartDate | date:"MMMM dd yyyy ', ' h"}} {{Event.EndDate | date: "'-' h:mm a"}}
Note: you could save yourself from parsing the left side of the "-" with Angular's 'longDate', but that will add a comma after the day of the month (e.g. September 20, 2017 vs September 20 2017).
Check out the docs for detailed info on which date parsing character does what.
Just use JavaScript, you have the split function (ex: .split(/[-T]+/)). For the month you have the 09 number, create your own function to get the month by array's indexes.

Making Excel Recognize Custom Day/Time Format

I'm working with a date and time format from the Twitter API. It looks like this:
Tue Nov 26 20:44:15 +0000 2013
Is there a formula to convert this to a format that could be sorted chronologically? I don't need the +0000. Also not concerned about the day of week.
=DATEVALUE(MID(A1,9,3) & MID(A1,4,5) & RIGHT(A1,4)) + TIMEVALUE(MID(A1,11,9))
Then format as you like. As requested you would want a Custom Format of mmm dd HH:mm yyyy
Try this, assuming that your string is in A1:
=DATETIME(MID(A1, 5, 16) & MID(A1, 27, 4))
The two MID formulas cut out the parts of the string you want, namely the month, day, time, and year, but exclude the day of the week and the timezone. This produces a string that the DATETIME function can automatically recognize and covert into a native Excel date format.

Resources