jQuery / Javascript - convert datetime to user's timezone datetime - javascript

I updated this question
I would like to transform a datetime to the equivalent datetime in the country(timezone) user is located to.
I have datetime in this format which is UTC/GMT:
Oct 31, 2012 08:10:02
now, according with the user's client timezone i would like to convert that datetime using the current browser/client user's timezone
how can i do that?

If I understand correctly, you want to be able to set the end date for the countdown in UTC time, as opposed to the local time of the browser. You don't need to modify the plugin to do this. You just want the setUTC... methods in the Date object. Take a look here: http://www.w3schools.com/jsref/jsref_obj_date.asp.
For example:
var endofworld = new Date(0);
endofworld.setUTCFullYear(2012);
endofworld.setUTCMonth(11);
endofworld.setUTCDate(21);
$('.countdown').countdown({ date: endofworld });

Related

converting time to local timezone not working with moment.js in React

I am converting a timestamp on a DB object using moment:
{moment(comment.created_at).local(true).format('h:mm a')}
My time is outputting in UTC time because that is how it gets created in my DB.
So I am seeing '6:45 PM' for example, when I want to see the time in MY timezone (EST) or the user's relative timezone. According to the moment docs local() will convert to your current timezone? Calling the local() method as shown in my code aboven does not change the time zone. Am I using this incorrectly?
My DB object
{
client_id: 24
created_at: "2022-02-11 17:41:39.330443"
id: 22
report: "sfsf"
report_category: "Client Assigned"
volunteer_id: 23
}
Your database is storing the date without an offset indicator. This means that moment cannot automatically determine the timezone. As per the documentation on parsing dates:
moment(...) is local mode. Ambiguous input (without offset) is assumed to be local time. Unambiguous input (with offset) is adjusted to local time. * moment.utc(...) is utc mode. Ambiguous input is assumed to be UTC.
So if you know your input is UTC and you know it won't have an offset indicator, use moment.utc() instead of moment().
Furthermore, you don't want to use local(true), since passing in "true" will only change the timezone on the object without changing the time (see the documentation). So you're left with:
{moment.utc(comment.created_at).local().format('h:mm a')}
I converted your DB timestamp into ISO format and then passed into your implementation
let t = "2022-02-11 17:41:39.330443"
let utcISOTimestamp = moment.utc(t).toDate()
console.log(utcISOTimestamp)
//let res = moment(utcISOTimestamp).local(true).format('h:mm a');
let res = moment(moment.utc(t).toDate()).local(true).format('h:mm a');
console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Try using:
moment.utc('2022-02-11 17:41:39').local().format('YYYY-MM-DD HH:mm:ss')

Convert back the time that was converted from js getTime() in PHP

I have frontend that accept date from calendar, with this format: 9/02/2021 11:00 AM
it then converted into these format: 2021-02-09 11:00, then it converted by this line of code:
var timeStamp = new Date('2021-02-09 11:00').getTime() / 1000;
I know these produce time in this value: 1612843200
Now in backend, i want to read that time value, and want to convert it back into Y-m-d H:i format, i tried date('Y-m-d H:i', '1612843200') but the result is 2021-02-09 04:00
my purpose is to get the time back to date format, and set timezone to UTC later, but i stumble in the first part before set timezone to UTC
how can i get the date back again?
You can use Intl.DateTimeFormat to get the timezone of the user.
See the below code to get user timezone.
let userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(userTimeZone);
Then you can send the user timezone, along with the seconds that you are using to your backend server.
There you can set the date_default_timezone_set using the timezone you are getting from the frontend.
See a list of timezone here.
Based on your need you can change the date-time accordingly at the backend.
In your PHP, you can try something like this
$userTimezone = $_POST["userTimeZone"];
$userSeconds = $_POST["seconds"];
date_default_timezone_set('UTC');
//Set this to get UTC timezone
You can use the following code,
date('j/m/d H:i A', '1612843200')

Converting a DateTime Object to a specific timezone

I have a dateTime string "2019-02-14 17:18:22".
I would like to convert this above-mentioned dateTime string to a specific timezone dateTime .
Here the timezone will be extracted from another dateTime string - "2019-02-14T17:28:24+08:00".
I did look up at the utcOffset function but I don't know how to use the offset value (330 in mycase).
Expected result: The first String is fairly simple 5:18 PM .
But once getting converted to the specific timezone, it will be 2:48 PM.
Heres how I use timezone offsets.
I get DateTimeIn, which is offset to (UTC+00:00) from the server.
Then to convert to the browser's timezone (UTC-05:00), i use: getTimezoneOffset() to update the object to the local timezone.
var dateObj = new Date(DateTimeIn);
dateObj.setMinutes(dateObj.getMinutes() + dateObj.getTimezoneOffset());
Without this, my server downloaded datetimes are offset and unusable.

Converting between timezones without affecting the actual time?

I am trying to store and retreive a date object that is supposed to remain consistant on saving regardless of whatever timezone the browser is set to.
For example. I have a 7PM IST which when converted with an offset should return to 7 PM of a timezone that I select.
I then want to be able to retreive the same timestamp as 7 PM of whatever timezone the browser is in.
I have figured out the first part
var date = moment(date);
var localDate = date.clone();
localDate.tz(timezone); // continent/city from momentjs
localDate.tz(timezone);
localDate.add(date.utcOffset() - localDate.utcOffset(), 'minutes');
localDate.toDate();
which ultimately gives me the date and I can use to save into the database as UTC ( I am saving it in mongodb)
I am not sure on how I can reverse it back to the local timezone so that I can get the return value as 7PM of the browsers timezone.
convert the date into UTC format before you save to db
moment.utc()
Whenever you retrive convert from UTC to local time.
moment.utc(utcDateTime, utcDateTimeFormat).local().format(specifiedFormat)

MomentJS set timezone

Because of how we store dates I need to set a moment object to timezone +0000.
I've tried using a variety of ways:
var d = moment().hour(0).minute(0).second(0).millisecond(0).zone('+0000');
var d = moment().hour(0).minute(0).second(0).millisecond(0).utc(0);
var d = moment().hour(0).minute(0).second(0).millisecond(0).utc();
When I console.log these dates they come out with the time 00:00:00 GMT+0100 (BST)
Looking at the documentation it seems to say that .utc() and .zone() are for printing a format only, is this true? (This is the same I've seen with other questions on here, none address setting the actual object to a timezone it seems)
After I set and then manipulate the date I convert it to the JS Date object to use with angular-ui bootstrap datepicker (note: it was a moment object which I used console.log on).
Unless you specify a timezone offset, parsing a string will create a
date in the current timezone.
http://momentjs.com/docs/#/parsing/string-format/
Example: You can tell in which timezone is your date using
moment ('2015-05-06T23:00:00.000Z').
If you need to convert this to a specific timezone you can do so:
moment().utc(0).format('YYYY-MM-DD HH:mm Z').
About Timezone in Javascript: How do I specify the time zone when creating a JavaScript Date?

Categories

Resources