Issue with Javascript Date Daylight Time Saving and oracledb-node - javascript

I've some problems with extracting DateTime field from a database using oracledb-node driver.
I've in the database two records with DateTime field like this.
# |MY_DATE |SESSIONTIMEZONE
1 |2020-05-02 00:00:00|Europe/Berlin
2 |2020-03-02 00:00:00|Europe/Berlin
When I retrieve it with oracledb-node to convert it into JSON, the second record changes day.
First Record
Rest API (JSON)
"2020-05-01T22:00:00.000Z"
Javascript Date
Sat May 02 2020 00:00:00 GMT+0200 (Ora legale dell'Europa centrale)
Second Record
Rest API (JSON)
"2020-03-01T22:00:00.000Z"
Javascript Date
Sun Mar 01 2020 23:00:00 GMT+0100 (Ora standard dell'Europa centrale)
I suppose that I'm getting in trouble with Daylight Saving Time.
Can someone help me with the right approach?
Thank you.

Does this help?
const dates = ["|2020-05-02 00:00:00|Europe/Berlin", "|2020-03-02 00:00:00|Europe/Berlin"];
const DateTime = luxon.DateTime;
dates.forEach(str => {
const [, dStr, tz] = str.split("|")
const d = DateTime.fromISO(dStr.replace(" ", "T"), {
zone: tz
});
console.log(d.toISO());
console.log(d.toUTC().toISO());
})
<script src="https://cdn.jsdelivr.net/npm/luxon#1.24.1/build/global/luxon.min.js"></script>

According to #Cristopher comment to my question, the problem was that my node.js backend code was working with a different Timezone compared to the one set in the oracle database.
Following the node-oracledb documentation about How to fetch Datetime fields, I've set the environment variable ORA_SDTZ='Europe/Berlin' according to the timezone of the database.
Some quick tips to help the troubleshooting:
How to get the database Timezone
SELECT sessiontimezone FROM DUAL;
How to get the node.js environment TimeZone
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

Related

get current date in SQL query in JavaScript

I am writing a method in lambda using node.js and I have SQL Query in that method and I want current time so I will get that
here is my query I am using in that function
let sql =`UPDATE ctrData2.Tag SET Name=?, Description=?, UpdatedBy=?, UpdatedDate= now() WHERE TagId=?`;
SO what is the right way to get the current time?
JavaScript has Date object for working with dates:
let now = new Date();
This gives you the current Date, e.g.
Tue Jul 07 2020 10:42:56 GMT+0300 (East Africa Time)
Take a look at the documentation for the various ways you can manipulate Date objects.

Date in Javascript and Java(Spring boot)

I've read many posts on this topic, still i have some questions
first i get an date input (28/09/2019) from html input tag, let's call it aDate
<input name="ExpireDate" ng-model="personalDetail.ExpireDate" type="date">
in Javascript
console.log(aDate) gives Sat Sep 28 2019 00:00:00 GMT+1000 (Australian Eastern Standard Time)
console.log(aDate.toISOString()) gives 2019-09-27T14:00:00.000Z
i know they are both correct as the first one is UTC+10 time and the second one is UTC+0 time
and then i pass aDate to a spring boot application, it shows 2019-09-27T14:00:00.000Z if i parse it to LocalDate(which is supported by postgresql) it will lose T14:00:00.000Z part
then if i try to convert LocalDate 2019-09-27 in Java to Date in Javascript the date is now one day off.
my current solution is use LocalDateTime instead of LocalDate, it worked but i really dont want to store that time info in database
is there a way to get rid of the time info in Javascript Date? or any other solutions to tackle this
another strange things is why i specified type="date" in html input tag, it still give me datetime data?
UPDATE:
LocalDateTime does not work
converting 2019-09-27T14:00:00.000Z to LocalDateTime yields 2019-09-27T14:00
but in Javascript new Date('2019-09-27T14:00') treats it as local time and yields
"Fri Sep 27 2019 14:00:00 GMT+1000 (Australian Eastern Standard Time)"
"2019-09-27T04:00:00.000Z"

Object property looking different after printing on it's own

I am trying to use luxon to generate a new date using a timezone. This is my code:
var luxon = require('luxon');
luxon.Settings.defaultZoneName = 'UTC+4';
var date = luxon.DateTime.local();
console.log(date);
var now = new Date(date.ts);
console.log(now.toString());
And this is the console:
DateTime {
ts: 2018-09-13T13:09:45.333+04:00,
zone: UTC+4,
locale: en-US }
Thu Sep 13 2018 11:09:45 GMT+0200 (CEST)
But if I try to access the ts property like so
var date = luxon.DateTime.local();
console.log(date.ts); // here
var now = new Date(date.ts);
console.log(now.toString());
I get this in the console:
1536830052009
Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)
Why is that? Is it doing some kind of math in the background? Also it turns out this date.ts is just ignoring my timezone. How can I fix that?
First 1536830052009, This is your time in milliseconds,
new Date(1536830052009)
// output Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)
You may want to check your timezone with getTimezoneOffset()
Returns the time difference between UTC time and local time, in minutes
Many people use moment.js to play with Date, I know it is not in your question but maybe you could find some usefull things
ts is not a public property and you shouldn't use it. Luxon does all sorts of tricks under the covers to get the math right. If you want the timestamp, just use date.toMillis(). If you want a JS Date, use date.toJSDate().
Two other important things to know:
It's not ignoring your zone. The zone doesn't change the time. It's more like metadata about a time that affects how we display it. The Luxon docs cover this a bit. You shouldn't expect to extract a different timestamp by fiddling with the zone. Now is always now.
Remember that the native Date object doesn't support timezones other than your local one. So anytime you convert from a Luxon object to a native Date, that information is lost. The time itself will be the same (meaning, it will represent the same millisecond), but it will express it in the local time.

moment.js - which one would be the correct output, considering that my timezone is GMT-3?

For some reason, when I retrieve a previous saved date from the database and try to convert it to my local time, it differs from the current time.
This is my code:
// got accessToken from database
console.log(moment(accessToken.expiresAt).toDate());
console.log(moment().toDate());
The output is:
Fri Aug 25 2017 20:47:51 GMT-0300 (BRT)
Fri Aug 25 2017 17:47:51 GMT-0300 (BRT)
Considering that my current local time is 25/Aug/2017 17:47:51 and that the accessToken.expiresAt was saved in the same request (at the same time), which one would be the correct output?
And if someone have any idea of why my stored time (stored as timestamp in mysql) is differing from the current time, please lead me to a direction.
Thank you in advance.
Moment.js probably assumed that the access token time you saved was in UTC - try this:
console.log( moment( accessToken.expiresAt ).utc().toDate() );
You can check the difference between your local timezone and UTC by entering your timestamp in a date converter such as http://www.convertunixdate.com - to get your timestamp, execute this:
moment( accessToken.expiresAt ).unix()

JavaScript change timezone

Hello I am using Twitter REST API to call the home_timeline for timelines.
But I found that the object it returned had the created_at with GMT+0 timezone, how can I adjust the timezone to the user's local timezone for example GMT+8?
Here is the original data with GMT+0 from Twitter API:
Wed May 04 16:23:13 +0000 2016
what's the expected output format?
you can try:
var d = new Date('Wed May 04 16:23:13 +0000 2016');
var localeTime = d.toLocaleString();
console.log(localeTime);
if you expect a specific output format, you can use http://momentjs.com/timezone/ , to change both the timezone and format
I'm highly recommend to use MomentJS timezone library.
Furthermore, I'm recommend not to use GMT+8 label for time shifting, and use, for example, "Asia/Shanghai" instead. I would save hours to debug for you.
So, code barebone look like:
let moment = require("moment-timezone");
moment("Wed May 04 16:23:13 +0000 2016").tz("Asia/Shanghai").format('YYYYMMDD hh:mm:ss')
Output would be something like "20160505 12:23:13".
Thanks!

Categories

Resources