Convert Javascript Date object to other timezone Date Object - javascript

How to convert Javascript Date Object to another timezone but the result must be Date object with the correct timezone
let date = new Date();
console.log(date);
date = date.toLocaleString('en-US', { timeZone: 'America/Vancouver' });
date = new Date(date);
console.log(date);
that gives the following result, the last result line (Date/Time) is correct but the time zone is incorrect which is still GMT-0500 (Colombia Standard Time) but must be GMT-0800 (Pacific Standard Time) timezone
Wed Jan 20 2021 00:14:11 GMT-0500 (Colombia Standard Time)
Tue Jan 19 2021 21:14:11 GMT-0500 (Colombia Standard Time)

You may try this :
let date = new Date();
console.log(date);
date = date.toLocaleString("en-CA", {
timeZone: "America/Vancouver",
timeZoneName: "long",
});
console.log(date);
Output:
Wed Jan 20 2021 09:18:16 GMT+0300 (Arabian Standard Time)
2021-01-19, 10:18:16 p.m. Pacific Standard Time
Once you get the correct TimeZone, you may change how the date and time are displayed by string manipulation if you need too.
Update:
This may not look pretty but i believe it should satisfy the requirements:
let date = new Date().toLocaleString("en-US", {
timeZone: "America/Vancouver",
timeZoneName: "short",
});
let date1 = new Date(date);
//adding a new property to Date object called tz and initializing it to null
Date.prototype.tz = null;
//stting the tz value to the Time zone output from toLocalString
date1.tz = date.slice(date.length - 3);
console.log(date1.toISOString() + " " + date1.tz);
console.log(date);
console.log(typeof date1);
Output:
2021-01-20T09:01:06.000Z PST
1/20/2021, 1:01:06 AM PST
Object
What i've done is create a new property of the object date to replace the built-in time zone property in Date, hence you get an object with a user specified Time zone.

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
}

Javascript Date comparison coming from server and new Date()

I have a date coming from database which is 2022-10-31 11:33:07.861Z, and in my js file I have this date is saved in a variable :
function test() {
let now = new Date().toLocaleString('en-GB', { timeZone: 'Europe/London'}); // I need now to be Europe/london time
let dateFromDb = db_date; // 2022-10-31 11:33:07.861Z
// I have a function which takes ^ above date and returns this : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
let formatedDate= this.toDate(dateFomDb); // returns : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
}
When I console.log(now, typeOf now) I get this: 02/11/2022, 23:24:52 string
and console for formatedDate returns this : Mon Oct 31 2022 11:33:07 GMT+0000 Object
I need to compare both dates :
return newDate <= now; // expect it to true or false;
Can anyone please suggest me how can I do this ?
I tried to cast both dates toLocalString but its string from I need object.

get UTC date using moment in javascript

I am getting a utc date using utcDate = moment.utc(new Date()).format(). But this utcDate is a string, not Date object. By using new Date(utcDate),it is again converting utc date lo my local date. Please help me in getting utc date object.
I work in javascript.
utcDate = moment.utc(new Date()).toDate() is converting it to my local date Sun Sep 01 2019 05:30:00 GMT+0530 (India Standard Time)
Using moment.js
var date = moment();
console.log(date.format()) // 2019-08-30T11:08:27+05:30
date = moment().utc();
console.log(date.format()); // 2019-08-30T05:38:27Z
Using Javascript
var dateObject = new Date();
dateObject.toLocaleString()
"8/30/2019, 10:55:19 AM" // My current time
var utcDateObject = new Date( dateObject.getUTCFullYear(), dateObject.getUTCMonth(), dateObject.getUTCDate(), dateObject.getUTCHours(), dateObject.getUTCMinutes(), dateObject.getUTCSeconds() );
utcDateObject.toLocaleString()
"8/30/2019, 5:26:04 AM" // UTC time which is 5.5 hours less than my local time

Is there an external api to get the current date/time in British Summer Time format

I have the following javascript function, where i am getting 2 variables from our system (expireddate + modifieddate)+ i am getting the current Date, as follow:-
function(){
var expirydate = item.get_item('ExpireDate');
var modifieddate = item.get_item('Modified');
var currentdate = new Date();
now in my case all the variables will be in this format Tue Jun 11 2019 00:00:00 GMT+0100 (British Summer Time).. and i am not facing any issue since my pc is also using the (British Summer Time) date, so the var currentdate = new Date(); will be compatible with other formats... but my question is how i can guarantee that var currentdate = new Date(); will always be in the (British Summer Time) date regardless of the users' PCs settings? so if the user is using another date format, to have the var currentdate = new Date(); in (British Summer Time) date format ? can i for example get the current date from external source?
I think I understand what you are looking for, and if so, you will want to use toLocaleString(), and tell it what format you would like the result to be in.
The following will result in british time format in the UTC timezone.
let date = new Date()
console.log('UTC:', date.toLocaleString('en-GB', { timeZone: 'UTC' }))
console.log('Guernsey:', date.toLocaleString('en-GB', { timeZone: 'Europe/Guernsey' }))
console.log('Guernsey Time:', date.toLocaleTimeString('en-GB', { timeZone: 'Europe/Guernsey' }))

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