How to get specific timezone datetime value using momentjs - javascript

I have a Event Created datetime value in DB which is actually time corresponding to "America" while entered.
And i want check if the event is already expired in client side javascript and i'm using momentjs
The catch is how will i check the expired event properly if i'm in a different time zone (for ex: india).
var eventDate = "05/06/2014 12:38 AM"
moment(eventDate).diff(moment())
instead of moment() how can i get moment object of specific time zone?
UPDATED
http://momentjs.com/timezone/
Need to include moment timezone js file and try below code
moment().tz("America/Los_Angeles").format("MM/DD/YYYY hh:mm a")

You can force eventDate to be formatted in a specific timezone:
var eventDate = "05/06/2014 12:38 AM GMT-0400" would result in moment(eventDate) offset to the local timezone.
See this jsfiddle: http://jsfiddle.net/x4hQ2/

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')

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)

Convert hour to date time without adding +1

Hi im using moment js to convert this string 20:00 I tried:
var a = moment("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00+01:00
the problem when I store in mongodb it become
2016-09-10T19:00:00.000Z
I want to store 2016-09-10T20:00:00.000Z
anyway can explain why please ?
When you say that you want to store 2016-09-10T20:00:00.000Z what you are saying is that you want to assume that your date and time is UTC.
To assume that the date you are parsing is a UTC value, use moment.utc
var a = moment.utc("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00Z
Note that when you parse a time without a date, moment assumes the current date. This may not be the behavior that you want.
I'm also not sure if you want a UTC date (which is what you are saying), or a local date without an offset indicator. If you want a local date without an offset indicator, simply use a format without an offset:
moment.utc("20:00", "HH:mm").format('YYYY-MM-DDTHH:mm:ss.SSS')
"2016-09-08T20:00:00.000"
If you are dealing with local dates that do not have a time zone association, I recommend using moment.utc to parse, as this will ensure that the time does not get shifted to account for DST in the current time zone.
For more information about how to parse dates into the time zone or offset that you would like in moment, see my blog post on the subject.
This it how it should look:
var a = moment("20:00", "HH:mm")
console.log(a.utcOffset('+0000').format())
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Doe, the problem is that you are using timezones when you create the date.
MomentJS uses your current timezone automatically.
Mongo however saves the time as it would be in another timezone.
Therefore, if you want the two strings to format the same way, you need to set the timezone.

Keeping the time fields but changing the timezone with moment.js

I have a moment object to which I need to apply a timezone, but without altering the values of the hour or minute field. For example, if my moment contains the date '2013-10-10T15:00:00+00:00' I want to be able to change the zone to 'America/Los_Angeles' so that when I print the moment I obtain '2013-10-10T15:00:00-07:00'
(for those of you familiar with Joda I'm after the withZoneRetainFields() functionality)
Here is a jsfiddle with the basic setup, showing the problem. How can I alter the last item so that it gives the desired output?
I recommend looking into the moment-timezone.js library for managing timezones. It offers an interesting .zone() function for manipulating timezones on a moment date object.
I've included a link to the relevent issue on moment.js's GitHub issues:
Switching timezone of moment without changing the values
Note, though I don't recommend it, you can also manually adjust a date for the desired timezone. In the below example, I'm creating a date in the client's timezone, changing the timezone on the date object to UTC, then fixing the value of the date such that the hours and minutes retain the same value.
// Takes a moment.js date object, and converts
// it to UTC timezone, while maintaining the
// selected hour/mins of the input date.
function convertDateToUTC (date){
// Current timezone's offset. Minutes offset from UTC
var offset = date.utcOffset();
// Convert selected date to UTC
date = date.utc();
// Adjust the time to.
date.add(offset, "minutes");
return date;
};

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

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 });

Categories

Resources