Convert Excel Date string to Unix time in Node.js - node.js

I use node-xlsx for parsing Excel file in Node.js but Date cells have strange view before parsing:
in Excel: 01.03.2016 07:44:04
in Node.js before parsing: 42430.32226851852
How I can convert this string 42430.32226851852 to Unix-time format?
Update:
Write myself solution that work for me:
var xlsxDate = '42430.32226851852'
var splitDate=xlsxDate.split('.');
var unixTime=new Date(1900, 0, splitDate[0]-1, 0, 0, Math.round(splitDate[1]/1157410)).getTime()/1000
console.log(unixTime)
Where 1157410 is ~1 second

i use this code to convert Unix-time format:
date = new Date(( parseInt(excelDate) - (25567 + 2))*86400*1000)
unix = date.getTime()

Another work around is that when you save it to your excel document you save it as a string and not a Date object.
Then you can read it as a string and just wrap it in a new Date()

Related

Reformatting date values when using them as URL parameters in a PowerQuery API request

I have two dates in my Excel table with the following format: "dd-mm-yyyy". These dates need to be sent as URL query parameters to an API endpoint for getting some data using PowerQuery. However, the API endpoint does not accept dates in that format. Therefore, I need to convert them to the format "mm-dd-yyyy" instead for it to work.
For getting the values from my table, I use the following code:
let GetNamedRange=(NamedRange) =>
let
name = Excel.CurrentWorkbook(){[Name=NamedRange]}[Content],
value = name{0}[Column1]
in
value
in
GetNamedRange
This function, called "GetValue", is then called when inserting URL query parameters in my GET request:
Csv.Document(Web.Contents("my.api/leave/leavecsv", [Query = [periodStart = GetValue("periodStart"), periodEnd = GetValue("periodEnd"), department = GetValue("department")]]),[Delimiter=";", Columns=14, Encoding=1252, QuoteStyle=QuoteStyle.None])
Currently the cells for my dates are in Text format. I tried using Date.FromText(...) to format the dates, but I get an error saying the datetime format is invalid.
https://learn.microsoft.com/en-us/powerquery-m/date-fromtext
How can I propertly format my date values before inserting them as URL query parameters using PowerQuery?
Ensure your dates are real dates and set to type date. then you can use the Date.ToText function:
let
theDate = #date(2022,12,7),
output = Date.ToText(theDate,"MM-dd-yyyy")
in
output
If, for some reason, you must maintain your dates as text strings (I'd like to know why, if that's the case), you can convert them first to a "real" date, and then create the string:
let
theDate = "13-12-2022",
output = Date.ToText(Date.FromText(theDate, "en-150"),"MM-dd-yyyy")
in
output
Make sure you pass in a culture and format. i.e.
Date.FromText([Column1], [Format="dd-MM-yyyy", Culture="en-UK"])

Python format incomplete date to YYYYMM

As a start, I am extremely new at Python.
I am receiving an Excel file where the date field is incomplete. The value displays as "190808" (YYMMDD) instead of "2019-08-08".
Part of my automation attempt is to move the file to a different location, where the file is renamed. I want to use the date field to change the file name to the file description and date (e.g. "Sales figures 201908").
The code I have only works if the date format is
str(df['Bank date'][0].strftime("%Y%m"))
I have tried dateparser with the following:
dateparser.parse(df['Bank date'][0].strftime("%Y.%m"))
The error I am receiving is 'numpy.int64' object has no attribute 'strftime'
Any help will do.
Thanks.
I modified it slightly and built my own date-string using slicing.
vOldDate = str(df['Bank date'][0])
vNewDate = '20' + vOldDate[:2] + '.' + vOldDate[2:4]
Numpy is interpreting the date as an integer. To use dateparser, you need to convert that value into a string first, then parse that string, and then format the result:
dateparser.parse(str(df['Bank date'][0])).strftime("%Y.%m")
Since the input format is expected, you should specify it to ensure you get the right date:
>>> dateparser.parse(str(190808), date_formats=['%y%m%d']).strftime("%Y.%m")
'2019.08'

gembox.spreadsheet date gets converted to int

I am loading an Excel 97-2003 XLS file into C# using GemBox.Spreadsheet version 39.3.30.1202.
One cell has the value 13-01-2017 (Formatted: 20170113) and the datatype "Date" with custom formatting "YYYYMMDD".
After
Gembox.Load(new XlsLoadOptions( ) { PreserveOptions = XlsOptions.None });
The cell has the value 42748 and valuetype 'int'.
I can get the correct value using ExcelCell.ConvertExcelNumberToDateTime(cell.DoubleValue, false)
= {13/01/2017 00:00:00}, but since this is donw in an automated process I wouldn't be able to tell real integers from the wrongfylly converted date.
How can i get GemBox.Spreadsheet to see that the date is not a number?
This was (hot)fixed in GemBox.SpreadSheet 39.3.30.1205 nuget

How do you parse a date and time string with offset using NodaTime?

I am trying to learn how to use NodaTime within my application, but cannot find very many examples of how to do certain things with this library.
Given:
date/time text of "2012/08/30 17:45:00"
the format string is "yyyy/MM/dd HH:mm:ss"
the date/time offset from UTC is -5
How do I parse this with NodaTime to get an
OffsetDateTime?
Instant?
Using pure NodaTime code, there is not currently a direct parser for an OffsetDateTime. See the documented limitations. However, you can construct one by parsing a LocalDateTime and an Offset separately:
var ldt = LocalDateTimePattern.CreateWithInvariantCulture("yyyy/MM/dd HH:mm:ss")
.Parse("2012/08/30 17:45:00")
.Value;
var o = OffsetPattern.GeneralInvariantPattern
.Parse("-05")
.Value;
var odt = new OffsetDateTime(ldt, o);
There is a similar parser for Instant, but it requires a UTC time - not an offset.
You could also just use the text parsing for DateTimeOffset in the BCL, and then do:
var odt = OffsetDateTime.FromDateTimeOffset(dto);
Either way, once you have an OffsetDateTime, it is convertible to an Instant:
var instant = odt.ToInstant();

Converting UTC Date to GMT in Groovy / Java

I am working with SoupUI a i need to adjust a Date/time (UTC) that i get back in a response to a GMT Date/time. The date that i get back in the respone looks as followes:
2012-11-09T00:00:00+01:00
I would like to convert this to
2012-11-08T23:00:00Z
Unfortunatly i lack Java skils and therefore also Groovy skils to be able to do this on my own. i did a lot o searches on date convertions but until now i was still unable to find what i was looking for. i will keep searching. if i do manage to get the solution then i will post it here.
Assuming there isn't a colon in the timezone portion, I believe this should work:
// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'
// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )
// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )
// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )
// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'
If there is a colon in the TimeZone, you'll need Java 7 for this task (or maybe a date handling framework like JodaTime), and you can change the first two lines to:
// Your input String
String original = '2012-11-09T00:00:00+01:00'
// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )

Resources