I have no experience with JS, but I need to get the time from a node red flow where the payload return the date in this format : 2022-09-03 08:23:39.806170+00:00 and then compare to the sensor.time which is the current date to return the elapsed time.
so something like this VarElapsed to (MyPayload - sensor.time)
again I have no experience with JS so I would thing this would be the result but thatss probably doesn't make any sense...
var d=new Date();
var start_time=payload
d=new Date();
var end_time=d.getTime()
var difference =end_time-start_time;
The format is cool to instantiate a new Date object.
var payload = "2022-09-03 08:23:39.806170+00:00";
var start_time = (new Date(payload)).getTime();
var d = new Date();
var end_time = d.getTime()
var difference = end_time - start_time;
console.log("difference in ms: ", difference)
Related
I am probably making this harder than I need to.
I am using nodejs on the server. The front-end send me the offset.
I need the UTC equivalent of yesterday (or today, last week...), for example, based on offset.
Currently I have:
getYesterday(): DateRange {
const today = new Date();
const fromDate = format(addDays(today, -1), DATE_SERVER_FORMAT);
const toDate = format(today, DATE_SERVER_FORMAT);
return {
fromDate,
toDate
};
}
But this is all based on the server timezone. I need it based on the offset sent from the frontend.
So today needs to be in UTC. So if the offset is 420 (-7) then Yesterday needs to be '2020-05-19 07:00:00.000' to '2020-05-20 07:00:00.000' even if the server is in Guatamala.
My thoughts are to get today's date (not time) in UTC then add (or subtract) the offset. Then use that date to addDays to.
I'd rather not use an additional library.
Gina
I found the answer here: stackoverflow answer
var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());
Which allows me to create "yesterday's" date range:
getYesterday(offset :number): DateRange {
var today = new Date();
today.setUTCHours(0,0,0,0);
today = addMinutes(today, offset);
const fromDate = addDays(today, -1).toISOString();
const toDate = today.toISOString();
return {
fromDate,
toDate
};
}
var date1 = new Date();
var date2 = new Date();
console.log(date2.toUTCString());
console.log(date1.getUTCDate());
<h1>Date UTC +_ 0000</h1>
<pre>
You can see the date of output
console.log(date1.getUTCDate());
and
console.log(date2.toUTCString());
is Same
So the simple way to get UTC Date and Time is in built in Date API
</pre>
And to Manage with time difference or what we can say offset Use following script
var targetTime = new Date();
var timeZoneFromClient = -7.00;
var tzDifference = timeZoneFromClient * 60 + targetTime.getTimezoneOffset();
//convert the offset to milliseconds
//add to targetTime
//make a new Date
var offsetTime = new Date(targetTime.getTime() + tzDifference * 60 * 1000);
console.log(offsetTime);
I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)
Currently I'm doing the following:
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
Is it possible to avoid the second invocation of Date?
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
I am new to JavaScript and have done some research, however I cannot find a straightforward solution to relatively simple problem. I have a following var:
var recordDate = '2016-04-20 17:52:33';
I need to get the difference between now and recordDate in seconds. What would be the most efficient way to achieve it?
Try to convert recordDate to a Date Object first
var match = recordDate.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/);
var date = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]);
var currentDate = new Date();
var differenceInSeconds = Math.abs(date.getTime() - currentDate.getTime())/1000;
DEMO
var recordDate = '2016-04-20 17:52:33';
var match = recordDate.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/);
var date = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]);
alert(date.toString());
var currentDate = new Date();
var differenceInSeconds = Math.abs(date.getTime() - currentDate.getTime())/1000;
alert(differenceInSeconds);
Set the date record like this:
var recordDate = new Date('2016-04-20 17:52:33');
var currentTime = new Date();
And then calculate it like this:
(currentTime - recordDate) / 1000 = {Seconds in between}
Additional comment:
Additional explanation why I am not using abs is because it looks like the purpose for this it to display like where in stackoverflow - edited 5 seconds ago or similar, when the currentTime will never be less then the record date and therefore one calculation and dependency less.
I currently am using this to create a Unix time stamp for time (now) -1 year's time.
Can someone please share a better and more efficient way to do this?
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var lastYear = parseInt(currentYear) - 1;
var lastYearDateObj = new Date(lastYear, currentDate.getMonth(), currentDate.getDate(), currentDate.getHours(), currentDate.getMinutes());
var lastYearTime = lastYearDateObj.getTime() / 1000;
Thank you!
I don't understand what your division by 1000 is about. You can add it to the end if you like:
var date = new Date();
date.setFullYear(date.getFullYear() - 1);
// date.getTime() / 1000 // if you want.
function openAPage() {
var startTime = new Date().getTime();
var myWin = window.open("http://www.sabah.com.tr","_blank")
var endTime = new Date().getTime();
var timeTaken = endTime-startTime;
myWin.close()
document.write(startTime);
document.write(endTime);
document.write(timeTaken);
}
hi i want to see the date here "document.write(startTime);".. how can i convert
document.write( new Date(startTime) );
If you check the documentation for the Date object one of the constructors is
new Date(milliseconds)
This way you recreate a Date from the milliseconds passed as argument.
It counts milliseconds since 1 January 1970 00:00:00 UTC.
But keep in mind that the window.open will not wait until the window has loaded before continuing execution of the code. So your startTime and endTime variables will be always pretty close.
Create your startTime with both a time and date and all will be well.
Like this:
var startTime = new Date().getDate();
var myWin = window.open("http://www.sabah.com.tr","_blank")
var endTime = new Date().getDate();
var timeTaken = endTime-startTime;
You can still determine the time difference (elapsed time) between them, but will also have the date available to the end of your routine.
document.write(startTime);
document.write(endTime);
document.write(timeTaken);