I have a UTC date that needs to display the local time. The result of the code below shows the time as 9:30 PM UTC but this isn't what I want. If I add the prop: tz="America/New_York" the time converts properly, but it needs to change based on where the user is. I checked the docs but didn't see a way to add the tz prop without setting an exact location.
<Moment
format="M/D/YYYY- h:mm A z"
utc
>
2018-12-06T21:30
</Moment>
I can also not see any documented support for determining the user's location/timezone in the react-moment library. You will likely need another library to determine that, perhaps one such as https://www.npmjs.com/package/jstz.
You have to add the local attribute
<Moment
format="M/D/YYYY- h:mm A z"
utc
local
>
2018-12-06T21:30
</Moment>
Documentation in the library:
https://momentjs.com/docs/#/manipulating/local/
And in the react-moment
https://www.npmjs.com/package/react-moment#local
In the react-moment documentation I can't see the utc attribute.
When you put the 'utc' you are saying that the date is utc and with the attribute 'local' you call the function local of the library and that transforms the date in a local date in the timezone of the browser
For default the date is in the timezone of the browser, for that reason you have to say that the date is 'utc'. If you call 'local' without the utc it returns the same date.
Related
I am using antd. I have a form that i want to send instead of DatePicker Object i want only the string but i don't know how to get the string from the Object.
I don't have any state or value to change.
Object:
_d: Date Mon Oct 26 2020 13:03:25 GMT+0200 (Eastern European Standard Time)
_isAMomentObject: true
I find this but i am not sure if is the best way to do.
console.log(date._d.toISOString())
Results: (and just take only the date)
2020-01-01T11:26:21.765Z
Do you know if is a better solution for this just to take 2020-01-01.
Use Moment Js.
moment(date).format("YYYY-MM-DD")
I am fetching a datetime value from the back end and I use the following line of code to get the time apart from the date :
{moment(element.dateTime).format('h:mm:ss a')}
I just want to know how to adjust the time zone in the code above to be the same as my country's time zone. Is there a way to achieve this?
Thanks in advance.
Moment by default will parse the date to your local computer. https://momentjs.com/guides/#/parsing/local-utc-zone/
You can use the utcOffset method as below, it takes the GMT Offset value. Change '+05:30' to whatever value you want
{moment.utc(element.dateTime).utcOffset("+05:30").format('h:mm:ss a')}
Also, Moment by default parses the date to your local timezone.
You can also use moment-timezone for timezone purposes, it asks for timezone name instead of gmtOffset. If you wish to use format method of moment though, there are certain limitations to it.
Moment Timezone
It might be a super simple solution, but I am struglling with it for a while now.
I am using angular.js and using input time.
The server returns the following date convention:
shiftStarted = "2017-01-17 19:55:11"
I want to put it as ng-model in the input field:
<input type="time" ng-model="shiftStarted" />
The problem is that input time should receive a valid JS date format,
In order to try to solve it I used this solution I found on the web:
shiftStarted = new Date(shiftStarted.replace(' ', 'T'));
But the following is changing the timezone to UTC.
How can I simply put the date accurately in the input time field without handling tough timezone issues?
Best,
Omri
If your date has been formatted as YYYY-MM-DD, the browser will parse it as an ISO date. (the full format would be 2017-01-17T18:03:26+00:00 at the time of me writing this)
Since no timezone portion was provided (only a date), the browser assumes the timezone is UTC by default.
If you just want to parse a date without bothering with timezones, you could use MomentJS (as suggested by Ananth)
Code would be:
shiftStarted = moment(shiftStarted, "YYYY-MM-DD").toDate();
There is the angular-date directive:
<input type="text" datetime="yyyy-MM-dd HH:mm:ss" ng-model="shiftStarted">
This also include a parser service. New timezone additions too.
https://github.com/eight04/angular-datetime
You could try date.getTimezoneOffset()
I'm not quite sure how to set the timezone in angularjs for the date filter, which converts 1970-01-01 to 1969-12-31. I've seen this post, which explains the same problem in Java, but not sure how to approach it in AngularJs - any clues?
The JavaScript date is based on a time value that is milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.
source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
JavaScript Date type does not have timezone support* you need to use either ISO 8601 format or send timestamp in UTC.
You can also use a library like timezone-js or moment.js
The angular developer guide says this about timezones:
The Angular datetime filter uses the time zone settings of the browser. The same application will show different time information depending on the time zone settings of the computer that the application is running on. Neither JavaScript nor Angular currently supports displaying the date with a timezone specified by the developer.
You can make your own filter to use a specific timezone. Here is a simple one using moment.js to parse 19700101 with the GMT timezone:
app.filter('GMT', function() {
return function(input) {
return moment(input + ' +0000', "YYYYMMDD").format('MMM DD, YYYY');
};
});
Here is a demo: http://plnkr.co/l5zd9Z1tli2NLmgPTWhJ
Thanks everyone for participating.
After quite a bit of time trying to find an easy way to resolve the problem I've found the solution - by simply using Date and one of it's methods:
new Date(date).toISOString();
That does the job and is super short :)
I would like to set date to default time zone. At this moment my date looks like this: 2013-04-08T22:00:00 +02:00. I need to set my date to +00:00.
I tried to get the offset of my date and I received -120. Is it possible to set time offset? Are there maybe any better solutions?
Anybody an idea?
I'm using EXT JS.
JavaScript's Date object is bound to the time zone of the computer itself. There is no way to change the zone offset programmatically to an arbitrary value.
There is some work going on to add this functionality to the Moment.js library, but it is still in progress.
If you are just looking to get the time at UTC in ISO format, you can use .toISOString()
If you are speaking about code that is specific to the Ext.Date object from ExtJS, please edit your question and post some sample code so we can get a better idea of what you are talking about. Thanks.