Compare Date 1 and Date 2 with 10 minutes - javascript

This is how I calculate the duration:
var duration = new Date().getTime() - customerForgotPassword[0].createdTime.getTime();
This is how I compare:
var TEN_MINUTES = 10*60*1000;
if(duration > TEN_MINUTES){
//do smtg
}
new Date().getTime() returns 1528291351108 which is Wed Jun 06 2018 13:22:31 in UTC
customerForgotPassword[0].createdTime returns Wed Jun 06 2018 13:20:04 GMT+0800 (Malay Peninsula Standard Time) in my code.
customerForgotPassword[0].createdTime.getTime() returns 1528262404000 which is Wed Jun 06 2018 05:20:04 in UTC
In database, customerForgotPassword[0].createdTime is in UTC format but when I retrieve it, it shows the local timezone. How can I set this to UTC format too when I retrieve it in node.js using Sequelize ?

I had to add 8 hours to get time in UTC
var timeInUTC = customerForgotPassword[0].createdTime.getTime() - (customerForgotPassword[0].createdTime.getTimezoneOffset() * 60 * 1000)

Related

Having issue calculating timeSince due to timeZone confusion

So I am a little bit confused. the server return date is in UTC ISO format, my local time is CST.
so I use toLocaleString() and then subtract the difference but I get a negative number. I rather use react native library rather than installing new libraries such as moment
export function timeSince(date) {
//date is 2022-11-07T18:36:39.543 which is UTC, not CST
date = date.toLocaleString("en-US", {
timeZone: "CST",
});
// somehow stayed 2022-11-07T18:36:39.543
date = new Date(date);
//Mon Nov 07 2022 18:36:39 GMT-0600 (Central Standard Time)
Mon Nov 07 2022 12:29:21 GMT-0600 (Central Standard Time)
'2022-11-07T18:27:38.03'
var seconds = Math.floor((new Date() - date) / 1000); // -21506
return seconds
}

How do I add one day or 12 hours to date in format of Thu Mar 03 2022 12:00:00 GMT in JavaScript [duplicate]

This question already has answers here:
Add one day to date in javascript
(11 answers)
Closed last year.
I want to add one day or 12 hours to a date in the format of
Thu Mar 03 2022 12:00:00 GMT
I have tried:
new Date(value.startDate + 1);
but it does nothing.
Please help me out, I am new to JavaScript.
If you want to add something to your timestamp, this will do the trick no matter what you want to add.
const timestamp = new Date("Thu Mar 03 2022 12:00:00 GMT")
console.log(timestamp.toString())
// because getTime and setTime uses milliseconds:
const millisecondsToBeAdded = 12 * 60 * 60 * 1000
timestamp.setTime(timestamp.getTime() + millisecondsToBeAdded)
console.log(timestamp.toString())
Try this
const date = new Date("Thu Mar 03 2022 12:00:00 GMT");
date.setDate(date.getDate() + 1);

Convert date & time string to moment.js with timezone

I have user inputs of meeting_time which is javascript Date (calendar select) and meeting_time string (select dropdown option)
var userTime = meeting_time ? meeting_time.label.split(' -')[0] : null
var userDate = moment(meeting_date).format("YYYY-MM-DD");
How Do I converting those variable to moment.js object with specific timezone
For example,
meeting_date is Thu Jul 12 2018 12:00:00 GMT+0300 (MSK)
meeting_time is 15:00 - 15:30
I want to get moment _d Thu Jul 12 2018 20:00:00 GMT+0800 (UTC +3 to UTC +8)
You can set the hour and minute in the date.
const [hour, minute] = userTime.split(':');
moment(meeting_date).hour(hour).minute(minute).toDate();

How to get timezone without moment-timezone.js

I have this string "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" and I need to convert it to the timezone (GMT-0300) using just moment.js (not moment-timezone.js)
I'm doing this but it's returning the same hour.
var startDateTime = moment(date).format('YYYY-MM-DD HH:mm Z'),
startMoment = moment.parseZone(startDateTime).zone();
console.log(moment(startDateTime).zone(startMoment).format("YYYY-MM-DD HH:mm"));
Any help ?
To convert a moment object with timezone A to timezone B, you can do the following:
var startDateTime = "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)";
var newDateTime = moment(startDateTime).zone('-0400').format('YYYY-MM-DD HH:mm')
Also note that in your example, the initial time "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" is already GMT-0300.

Convert date to end of day

I get time in milliseconds from the server. I convert it to Date and get -
Mon Jul 22 2013 11:16:01 GMT+0200 (W. Europe Daylight Time) as the date in the record.
I want to separate out data of Monday, Tuesday etc into arrays. I am thinking of converting this date to Mon Jul 22 2013 23:59:59 GMT+0200 (W. Europe Daylight Time) and then filter out the records.
How can i change the date to the required end of the day time? or is there an easier way to do this ?
You could always construct a new DateTime object just using the year, month and day properties from the existing date, like so:
var actualDate = new Date(); // 2013-07-30 17:11:00
var endOfDayDate = new Date(actualDate.getFullYear()
,actualDate.getMonth()
,actualDate.getDate()
,23,59,59); // 2013-07-30 23:59:59
For future visitors, just use
var start = new Date();
var end = new Date();
start.setHours(0,0,0,0);
end.setHours(23,59,59,999);
Using http://momentjs.com:
var now = new Date().getTime();
var endOfDay = moment(now).endOf("day").toDate(); // Wed Jan 20 2016 23:59:59 GMT-0800 (PST)
var actualDate = new Date()
var eodDate = new Date(Math.floor(actualDate.getTime()/86400000+1)*86400000 + actualDate .getTimezoneOffset()*60000 - 1000)
where 86400000 are total milliseconds in a day
If two Date Objects are on the same day then they have the same Date String:
new Date('1374488161000').toDateString()
=> "Tue Jul 30 2013"
new Date('13744917610403').toDateString()
=> "Tue Jul 30 2013"
Although a rather naive method of comparing days, it's probably the simplest comparison.

Categories