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
}
Related
my default timezone is (UTC + 8:00) Kuala Lumpur, Singapore and the computations is working fine but when I change my pc timezone to (UTC-6:00) Central Time (US & Canada) the result of is off by 1 day.
In (UTC + 8:00) Kuala Lumpur, Singapore Time the output from this.referenceMilestones[key]) is 2022-10-20 and after using the getRefMileStoneDate function the result is
#result which is correct
refMilestoneDate-- Thu Oct 20 2022 00:00:00 GMT+0800 (Philippine Standard Time)
But in timezone (UTC-6:00) Central Time (US & Canada) , the result if off by one day
#result which is wrong , it is now 19 which is supposed to be 20
refMilestoneDate-- Wed Oct 19 2022 00:00:00 GMT-0400 (Eastern Daylight Time)
Any idea how we can solve this one that the date should be consistent regarding of the timezone ? Thanks for helps and ideas.
#Code
const refMilestoneDate = this.getRefMileStoneDate(calculateFields[i].referenceMilestoneName);
console.log('refMilestoneDate--', refMilestoneDate)
getRefMileStoneDate(key:string):Date{
if (this.referenceMilestones && this.referenceMilestones[key]){
console.log(' ' , this.referenceMilestones[key])
return new Date(this.convertDateStringToYYYYMMDD(this.referenceMilestones[key]));
}
return null;
}
convertDateStringToYYYYMMDD(dateString: any) {
if (dateString) {
const dateObject = new Date(dateString);
return dateObject.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '-');
}
return '';
}
JavaScript, especially at the client side, typically assumes you're working in local times. You probably want to work exclusively in UTC via the various UTC methods available in the Date object.
Otherwise, you'll want to look into use a date library such as Luxon which will allow you precise control of time zones both when parsing timestamps and when formatting them for display.
I get a date string as Fri Sep 17 2021 11:50:59 GMT-0400 (Eastern Daylight Time) from one place. I get it from 2021-09-17T11:50:59-04:00 in a second place.
I want to convert the first format to the second.
I am doing this in a crazy way, so I am thinking there must be a better one.
var d = new Date(`Fri Sep 17 2021 11:50:59 GMT-0400 (Eastern Daylight Time)`);
var iso = d.toISOString();
var time = d.toTimeString();
console.log(iso);
console.log(time);
var [date] = iso.split('T')
var [,localTime, timezone] = time.match(/([^ ]+) GMT([^ ]+)/);
var timezoneWithColon = timezone.replace(/(-*[0-9]{2,2})([0-9]{2,2})/,"$1:$2")
var desiredFormat = '2021-09-17T11:50:59-04:00';
var convertedFormat = `${date}T${localTime}${timezoneWithColon}`;
console.log(desiredFormat)
console.log(convertedFormat)
console.log(desiredFormat === convertedFormat);
The fiddle is over at https://jsfiddle.net/Dave_Stein/8tLv2g4j/.
2021-09-17T11:50:59-04:00 is an ISO-8601 date string.
toISOString should work, however it will convert the time to UTC. If you want this to format in your current timezone, you'll have to use a library such as date-fns:
import { formatISO } from 'date-fns'
formatISO(new Date(`Fri Sep 17 2021 11:50:59 GMT-0400 (Eastern Daylight Time)`))
// prints '2021-09-17T11:50:59-04:00'
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.
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)
How to format date and time like this in JavaScript ?
March 05, 2012 # 14:30 (UTC - 9:30)
I use this code to calculate EST time :
function getDate() {
var now = new Date();
var utc = now.getTime() + (now.getTimezoneOffset() * 60000);
return new Date(utc + (3600000 * -4));
}
I use the date-time-format that Tats recommended because doing it manually is a huge PIA.
var yourDate = dateFormat(getDate(), "mmmm dd, yyyy # HH:MM) + "(UTC -9:30)";
Keep in mind this isn't Daylight Savings aware.. and you are asking for UTC -9:30 in your format, but your function converts to -4. Also, I believe that now.getTime returns in UTC.. so you can just add your difference there.
JavaScript Date Format
Check out date.js! It's a really powerful little library for working with Dates in JavaScript.
To get today's date in EST, you can do something like...
var today = new Date();
today.toString(); // outputs "Wed Apr 11 2012 15:40:40 GMT-0500 (CDT)"
today.setTimezone("EST");
today.toString(); // outputs "Wed Apr 11 2012 14:40:40 GMT-0500 (CDT)"
Also, its worth mentioning to checkout moment.js. I think the two libraries complement each other.
If you do just
var now = new Date();
document.write(now);
you will get
Wed Mar 14 2012 20:53:06 GMT+0000 (GMT Standard Time)
Link1, Link2.
Is it what you want?