Parse If-Modified-Since Header (node.js) - node.js

I am trying to implement sending a 304 header for performance in a server hosting program I am writing, but I do not know how to parse the date of the If-Modified-Since header. I also would like to know how to find out if the If-Modified-Since date is older/newer than another date that I have in my code.

Just in case if someone comes across...
To parse date from "Last-Modified" you can use Date constructor that takes a date string.
You can also use Date.parse, which returns number of milliseconds since epoch (for invalid dates it returns NaN).
To print back date in format suitable for "Last-Modified" or "If-Modified-Since" header you can use Date's toUTCString() method.
var date = new Date("Wed, 17 May 2017 04:44:36 GMT");
var ms = Date.parse("Wed, 17 May 2017 04:44:36 GMT");
console.log('parsed date: ', date);
console.log('parsed date ms: ', ms);
console.log('If-Modified-Since: '+date.toUTCString());

To parse the date, use new Date(datestring) or Date.parse(datestring). To see if a date is newer or older than another date, use the greater than (>) and less than (<) operators.

Related

Netsuite compare 2 datetime in saved search criteria

I would like to send an email if my URL is expired.
For that, I have created a saved search and a custom transaction field.
My goal is to show the transaction record in the saved search, from the moment the date is expired.
I tried with formula date but it return only if the date is before today. But it can happen that we are the same day and that the url is expired for 1min or 1hour.
Ex: Today = 13/09/2022 3:00 pm and expiration date is 13/09/2022 2:59 pm
So I tried with Formula numeric to do:
{customfield} - {today} is less than 0
But Netsuite return an unknown error.
So I tried:
TO_CHAR((NVL({customfield},{today})-{today})) is less than zero
But it return this kind of value so it's always zero
+000000000 20:57:33.000000
or
-000000000 02:26:33.000000
or
+000000000 00:00:00.000000
How can I do to send an email when the DateTime from my custom field is in the past?
The formula type you select specifies the data type for the result returned. When you select "Formula (Date)" (formuladate) or "Formula (Date/Time)" (formuladatetime), NetSuite will convert the returned difference into a time interval formatted as a date or timestamp. For numerical comparison, use "Formula (Numeric)" (formulanumeric) instead.

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.

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

Resources