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.
Related
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)
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.
I am a newbie in javascript, come to my question.
I am using ionic 2 build application in which i am using date time picker for taking a time.
I am getting a a time format is in the "hh:mm" using time picker. eg(10:11) which is in string format and i am using Date() function which give me date is in something like
"Mon Aug 01 2016 01:32:03 GMT-0700 (Pacific Daylight Time)"
I want to replace "hh:mm"(01:32) from Date object with my string output of "hh:mm"(10:11) and then want to convert that into new Date object.
I also tried split and slice function but doesn't work.
If i get some guide about this will be very helpful.
I will be very helpful to all of you.
thanks
First of all, read up on Date.
In your case, the following code is a starting point.
var d = new Date();
d.setHours('10');
d.setMinutes('11');
Of course, you should exchange '10' and '11' with your picker data. Also, there are many other methods you can use on the Date object.
I have telerik:RadTimePicker control and I want to get the time using javascript. In telerik web site they have mentioned
var picker = $find("<%=RadDateTimePicker1.ClientID%>");
var view = picker.get_timeView();
alert(view.getTime());
But when I tried this I get all the details including time information.
Tue Feb 26 2013 05:00:00 GMT+0530 (Sri Lanka Standard Time)
What I really need is 05:00:00
Any easy way ?
the getTime() is returning you a JavaScript Date object. You will need to use the JavaScript Date constructs to extract the Hours:Minutes:Seconds kind of data. Here is a link to JavaScript date documentation: http://www.w3schools.com/jsref/jsref_obj_date.asp
you can just use the get_selectedDate() on the picker. This method too returns you a javaScript Date object. You can then extract the hours, minutes, seconds from the date object. we don't have any specific time string returning property on the picker object model.
I am using Facebook FQL to get photos and the time when they were created. The problem is am getting the created time as some long integer value e.g. 1306776492.
Now I am unable to convert it to proper date time using javascript.
Can anyone point me in the right direction
You can set a Date objects time with the setTime method.
var d = new Date();
d.setTime(1306776492*1000);
document.write(d);
returns
Mon May 30 2011 20:28:12 GMT+0300 (FLE
Daylight Time)
http://jsfiddle.net/niklasvh/ANRcm/
If you want the date to be printed in a different format, just have a look at the other methods available for Date()