I'm developing an app that lets users create dates/events in other timezones so I need to convert something like
2014-12-01T19:00:00.000Z to any particular timezone such as 'America/Phoenix'.
I'm using moment to handle it:
moment.tz(dt, 'America/New_York').format();
This outputs: 2014-12-01T14:00:00-05:00 which isn't what I want.
All I want to do is change the timezone not the time such that it would become 2014-12-01T19:00:00-05:00
Perhaps I could add the offset of 5 (depending on zone) before i convert the timezone so it would remain the same time? Maybe there is a better way?
Thanks, James
You should recognize that the Z in your string means that the timestamp should be interpreted as UTC. So if you change that to a different offset without adjusting the time accordingly, you are actually picking a different moment in time. Usually, that's not the right thing to do.
By adjusting the time to match the offset for the time zone, Moment is doing the right thing. It's interpreting the input string as UTC, and then adjusting it to the time zone provided.
It sounds like you would like to ignore the Z and treat the time as if it were already in a particular time zone. The best place to change that would be wherever the string is originally generated. If it's not in UTC, it shouldn't be putting the Z at the end in the first place.
However, you could certainly strip it off in JavaScript and get the result you're asking for.
var s = "2014-12-01T19:00:00.000Z";
moment.tz(s.replace(/Z$/,''), 'America/New_York').format();
You can use moment methods like:
startOf
hour
minute
get
Example:
moment.tz(dt, 'America/New_York').startOf('day').hour(dt.get('hour')).minute(dt.get('minutes')).format()
This will print the same hour and minute as the dt variable.
Hope it helps
Related
I am using the "MongoDB for VS Code" plugin in Visual Studio Code (v1.49.0) and am trying to reset the time of a Date object to 00:00:00:000 in the MongoDB Playground.
Here is the code:
var thisMonth = new Date();
thisMonth.setDate(1);
thisMonth.setHours(0, 0, 0, 0);
thisMonth;
However, the value output for thisMonth is:
2020-09-01T06:00:00.000Z
and not 2020-09-01T00:00:00.000Z as expected. This happens regardless of when the Date object is created. Does anyone else have experience or know why this might be happening?
Thank you.
MongoDB will always use UTC time (UTC-0 or Zulu/Zero time).
The hours you set are according to your local timezone, so in this case most likely North America (UTC-6). The result is given with a Z on the end indicating it is Zulu time (zero time).
To get the offset on your local machine you can use thisMonth.getTimezoneOffset();
To set the time to UTC time you use thisMonth.setUTCHours(0);
You will see that when setting the time to UTC time 0 you will actually get the 0 result you are looking for.
MongoDB does not have an understanding of timezones.
This is not related to MongoDB but normal JavaScript behaviour. See Date#setHours():
The setHours() method sets the hours for a specified date according to local time [...].
MongoDB transforms dates into their UTC representation before storing them. And you (or your server) seem to be located in a -6:00 time zone. The actual moment in time is correct, you are just seeing a representation that might not be what you expected.
I have a timestamp from the backend and I want to display it with momentjs per console.log() without timeshifting and completly indepented from my browsers timezone. I read many posts on stackoverflow but nothing works. All of my outputs have some timezone included.
let timestamp = "2019-11-19T07:05:00+01:00";
console.log(moment(timestamp).toISOString(true));
console.log(moment.utc(timestamp).format());
console.log(moment.utc(timestamp).toISOString(true));
console.log(moment.parseZone(timestamp).format());
console.log(moment.parseZone(timestamp).local().format());
console.log(moment.parseZone(timestamp).utc().format());
console.log(moment(timestamp).utcOffset(timestamp).toISOString(true));
console.log(moment.tz(timestamp, 'Europe/Berlin').toISOString(true));
console.log(moment.tz(timestamp, 'Europe/Berlin').format());
console.log(moment.tz(timestamp, 'Europe/Berlin').unix());
console.log(moment.parseZone(timestamp).format('MM/DD/YYYY HH:mm:ss'));
console.log(moment(timestamp).utcOffset("+0100").format('YYYY-MM-DD hh:mm:ss'));
expected output:
2019-11-19T07:05:00Z
The Timezone of the timestamp is: Europe/Berlin
My Browsers timezone is switched to something different.
I don't understand why this simple problem has no easy solution. :)
To meet the requirement you described (keeping the local time while changing the offset) you can do the following:
var result = moment.parseZone("2019-11-19T07:05:00+01:00").utcOffset(0, true).format();
console.log(result); //=> "2019-11-19T07:05:00Z"
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Step by step explained:
moment.parseZone("2019-11-19T07:05:00+01:00") // Parses the string, retaining the offset provided
.utcOffset(0, true) // sets the offset to zero while keeping the local time
.format() // formats the output, using Z to represent UTC
However - You should recognize that these are not the same moments in time. The output timestamp is one hour earlier than the input timestamp. This is explained in the documentation as follows (emphasis mine):
The utcOffset function has an optional second parameter which
accepts a boolean value indicating whether to keep the existing time
of day.
Passing false (the default) will keep the same instant in Universal Time, but the local time will change.
Passing true will keep the same local time, but at the expense of choosing a different point in Universal Time.
Thus, it is usually the wrong choice when you already have a specific point in time, either in UTC or as an offset from UTC (like your example input value).
Instead, you should expect that converting from local time to UTC will indeed change the date and time portion of the timestamp. You can use .utcOffset(0, false), or .utcOffset(0), or simply .utc() to do the conversion correctly.
I've a list of timespan(object list actually), like 2:00, 15:00, 18:00 etc, it is in utc.
Now i want to convert this time slot back to CST and then sort it, as i want my time sorted in cst.
For timezone conversion i needed temporary date. so i choose current utc date by
moment.utc(mytimespan). and performed the timezone conversion by .tz("CST").
So list is converted to 20:00,9:00, 12:00
Here please note that i got 20:00 in first place instead of last place in the list.
This is due to date part of moment which went in back date.
All here i want is my timespan in sorted form without any effect of date.
please me to find a way to do it without string conversion!
Thanks
Update
my currently working code using string conversion
TimeSpanDetails.sort(function compare(a, b) {
return moment(moment.utc(a.startTime).tz("CST").format("HH:mm"),"HH:mm").isAfter(moment(moment.utc(b.startTime).tz("CST").format("HH:mm"),"HH:mm")) ? 1 : -1;
});
Now i want to do it without string conversion using format
A few things:
A "time span" usually refers to a duration of time, not a time-of-day. These are two very different concepts that are sometimes confused. Consider:
A timespan of 99 hours is perfectly valid, but "99:00" is nonsensical as a time-of-day.
Due to daylight saving time and other time zone transitions, a timespan can't necessarily be thought of as "time since midnight" because midnight may or may not exist, or some other hour of the day may be absent or repeated.
Time spans can be negative in some programing languages, usually representing a period before a given point in time.
The tz function in Moment.js takes IANA time zone names. You should not use CT or CST, but rather America/Chicago, for example. However, time zones are completely unrelated to time spans, so you should not be applying them at all. You do not need moment-timezone.
Moment represents time spans in Duration objects. You can parse them from strings like so:
var d = moment.duration('99:00');
Duration objects convert numerically to milliseconds, so they are comparable like so:
var a = moment.duration('00:00');
var b = moment.duration('01:00');
var c = a < b; //=> true
Moment does not have a strongly typed object for a time-of-day, but you can use Moment in UTC mode so that it does not have DST transitions, and then just let it use the current day. HOWEVER:
This would assume that all time-of-day values you have should be evaluated on the same date.
This may or may not be the case.
Consider that if all you have is time-of-day and don't know what dates they're from, then the values ['23:00', '00:00'] may be sorted already and only one hour apart, or perhaps they're out of sequence and they are 23 hours apart.
I have a timestamp 2016-09-14T10:44:55.027Z and I would like to only display the 10:44:55 part but I'm not completely sure how. I have access to the moment library but not sure how to pass this into moment and format it, also how could I add AM or PM?
moment("2016-09-14T10:44:55.027Z").format('hh:mm:ss')
seems to output 11:44:55?
jsFiddle http://jsfiddle.net/eemfu0ym/
Since your input contains the Z suffix, that means the input value is in UTC. However, you're passing it into the default moment constructor, which is local time, thus a conversion occurs.
To keep it in UTC, the simplest way is to just obtain the moment object in UTC mode to begin with.
var m = moment.utc("2016-09-14T10:44:55.027Z")
Once you have that, you can format it however you like:
m.format('HH:mm:ss') // 24-hour clock time
m.format('hh:mm:ss A') // 12-hour time with meridiem (AM/PM)
See the moment formatting docs for other options. Do note that tokens are case sensitive.
I use the below code to format date time in iso format using java (I'm reducing 1 min from current time) and get the output as this "2016-03-17T11:38:21.xxxZ" < x represent some numbers> i want this to compare with the time which have mentioned in the DB.
Person who build that data insert query, he used javascript to get the time and format it in iso.
Date inside the DB is looks like this "2016-03-17T06:09:21.530Z" and its actual time is "11:39:21 GMT+0530 (India Standard Time)" which is similar to my current time but I'm comparing these two dates as string. and get 1min early data from DB.In that case i can't get an out put because as strings these two aren't match. can anybody recomand a solusion ?
I use OrientDB
Java Code
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Calendar date = Calendar.getInstance();
long t = date.getTimeInMillis();
date.setTimeInMillis(t);
date.set(Calendar.MINUTE, date.get(Calendar.MINUTE) - 1);
String time1minEarly = df.format(date.getTime());
Using Calendar.set() and Calendar.get() does not modify the date in a way you intend:
This will modify the minutes field in your case. So subtracting "1" will reduce the minute but not give a viable date for cases where minute initially is zero.
You may just subtract a minutes of milliseconds from your "t" variable to get a true minute offset.
And for ease of use you might also consider following advise from #Prashant and using LocalDateTime class from joda library.
Thanks Everybody for your support.
I figure out How to do this. it's pretty easy. Both #rpy and #Prashant are correct. Calendar is not suitable for solve my issue. Also LocalDateTime too. but it did help me to figure out the correct way.
#rpy and #Prashant they both did miss one thing that the javascript time represent the UTC time. that's the issue. (it's 5.5 hours behind compared to my location) so, I figure out this below code. it did what i wanted to do.
It's pretty easy. all you have to do is provide your zone id.
(You can get Zone id using this code : go to the link - http://www.javadb.com/list-possible-timezones-or-zoneids-in-java/)
Also you can choose so many formats by changing "DateTimeFormatter" value.
ZoneId UTCzoneId = ZoneId.of("UTC");
ZonedDateTime time1minEarly = ZonedDateTime.now(UTCzoneId).minusMinutes(1);
String UTCtime1minerly = time1minEarly.format(DateTimeFormatter.ISO_INSTANT);
Out put is similar to this : "2016-03-17T10:39:21.530Z"
(- UTC time at that time : 2016-03-17T10:40:21.530Z)