I have a DateTime string like 2/24/2017 17:00:00 which comes from a web service. The time is UTC time. Now i want to convert it to user's local time and display it in browser. And i'm using Moment.js.
I've tried:
var utcTime= moment('2/24/2017 17:00:00' + " +0000", "MM/DD/YYYY HH:mm:ss Z");
var localTime = utcTime.add(new Date().getTimezoneOffset().toString(), 'm').toDate();
It will work but somehow looks strange. Am i missing something? Is there a better way to do this?
Write it like this:
moment.utc('2/24/2017 17:00:00', "MM/DD/YYYY HH:mm:ss").local().format('lll');
Reference: http://momentjs.com/
Related
I have problem showing timezone with moment.js.
I tried with this code:
var result = moment(someDate).format("MM/DD/YYYY HH:mm A Z");
and I get return, for example: 08/05/2015 06:18 PM +02:00, which is fine, but I want that my output be like 08/05/2015 06:18 PM WEDT or something like that, with abbreviations of timezones.
Tried using this code, but I get empty timezone on the end:
var result = moment(someDate).format("MM/DD/YYYY HH:mm A z");
or
var result = moment(someDate).format("MM/DD/YYYY HH:mm A zz");
UPDATE
So as #Matt Johnson suggested, I used this approach to show time zone using moment-timezone-with-data.js and tzdetect.js:
var tzName = tzdetect.matches()[0];
var result = moment.tz(myDate, tzName).format("MM/DD/YYYY h:mm A zz");
As described in the documentation:
Note: as of 1.6.0, the z/zz format tokens have been deprecated. Read more about it here.
The general problem is that time zone abbreviations are not available from the browser through a consistent API. In order to provide them, one has to have an external source of data.
You may want to look into using the moment-timezone addon. It provides time zone information, including abbreviations. You would have to know the specific time zone you are working with. For example:
moment.tz("2015-08-05T00:00:00+01:00", "Europe/London").format("MM/DD/YYYY hh:mm A z");
// "08/05/2015 12:00 AM BST"
Also, you shouldn't mix HH (hours of the 24-hour clock) with A (the 12-hour am/pm designator). Either use hh with A, or use HH without A.
To Convert UTC datetime to user's current timezone the solution is to use moment-timezone instead of moment.(We have all dates as UTC in DB). Other timezones shouldn't be an issue either.
const timeZoneString = Intl.DateTimeFormat().resolvedOptions().timeZone
const getFormattedDateTimeWithTZ = (date) => {
return moment((date)).tz(timeZoneString).format('ddd, MMM DD YYYY, h:mm A zz')
}
// outputs Tue, Mar 08 2022, 4:00 PM PKT
getFormattedDateTimeWithTZ('2022-03-08T03:00:00.000-08:00')
I have a date like this:
2014-04-23T19:45:39 which is a UTC format.
I want to convert it to AST format or the localize time zone of the user. How to do it?
I suggest you use moment.js library and just add or subtract number of hours that AST time have comparing to UTC.
new_date = date.add('hours',4);
or
new_date = date.subtract('hours',4);
using timezone-js. you can easily convert the time from one timeZone to another. timezone.js
var date_object;
function localize(t){
date_object=new Date(t+" UTC");
document.write(date_object.toString());
}
localize("4/24/2014 4:52:48 PM")
document.write(date_object.toString().replace(/GMT.*/g,""));
Demo
Suppose i get the value '2014-03-03 16:20:20' as an input, I'd like to convert it
to it's real time with timezone (i.e, node sits in Europe , but timestamp was collected in America), and after that I'd like to get the UTC representation of the real value.
Any idea what packages can help? Tried moment.js with no luck..
finally found the answer with momentjs-timezone ,
which is kinda funny since you wont get a event a hint about on the mainsite http://momentjs.com/timezone/ but you can find it on http://github.com/moment/moment-timezone
anyhow here's how you do it :
var tz0 = moment.tz("2014-03-03 16:20:20", "Asia/Jerusalem")
var tz1 = moment.utc(tz0);
console.log(tz0.format('YYYY-MM-DD hh:mm:ss'));
console.log(tz1.format('YYYY-MM-DD hh:mm:ss'));
and the output as you can see respectivly
2014-03-03 04:20:20
2014-03-03 02:20:20
// moment.js
var utc = moment('2014-03-03 16:20:20','YYYY-MM-DD HH:mm:ss', 'us');
// or timezone string pass from client
var eu = utc.zone("+02:00");
var eu = utc.zone("-02:00");
momentjs may help you. Like the following:
Could you format the input with ISO date plus timezone info, like 2014-03-03T16:20:20-05:00 (supposing you get from local time at GMT-5 timezone)
Then call: moment.parseZone('2014-03-03T16:20:20-05:00').zone(1).format() (supposing you want to get local time at GMT+1 timezone)
Hope that help,
Ron
Timezone is an elegant module for this.
var tz = require("timezone/loaded");
var formattedDate = tz(unformattedTimestamp, '%d %B %Y, %-I:%M %p', "Europe/Amsterdam");
You can use bootstraphelper to capture user's timezone.
I want to change a format of a date and time string. But moment.js changes timezone to my system timezone (+3):
// This is a string:
"2013-09-20 23:59:59 +0100"
// I want to change it to this:
"20-09-2013 23:59:59 +0100"
// This is what I do and what I get. 1 hour is added by force:
moment("2013-09-20 23:59:59 +0100").format("DD-MM-YYYY HH:mm:ss ZZ")
"21-09-2013 01:59:59 +0300"
How to just change a format without changing timezone?
See moment issue #887, directly regarding this. It may be easier in a future version, but the current workaround is as follows:
var input = "2013-09-20 23:59:59 +0100";
var m = moment(input).zone(input);
m.format("DD-MM-YYYY HH:mm:ss ZZ")
In moment.js v-2.8.3:
var dateTime = "2014-12-09 13:59:59 +0930";
var parseDateTimeZone = moment.parseZone(dateTime).format("YYYY-MM-DDTHH:mm:ssZ");
Doc-API
I've created a date in JS like so:
var myDate = new Date('2013-01-01 00:00:00');
I assume JS reads this in as UTC time. But when I do something like myDate.getTime() the timestamp returned was something like 4AM GMT time.
Why is this? And how do I get the date as midnight in UTC time?
At least in Chrome, this works:
var myDate = new Date('2013-01-01 00:00:00 UTC');
It also works if you put GMT instead of UTC. But I don't know if this is cross-browser enough.
I live in India. Hence my timezone is the Indian Standard Time (IST) which is listed in the tz database as Asia/Kolkata. India is 5 hours 30 minutes ahead of GMT. Hence when I execute new Date("2013-01-01 00:00:00") the actual time at GMT is "2012-12-31 18:30:00".
I believe you live in America because you're in the EST timezone (GMT-04:00)? Am I right?
If you want to parse the time at GMT instead of your local timezone then do this:
new Date("2013-01-01T00:00:00+00:00");
Notice the capital T between the date and the time, and the +00:00 at the end. This is the format used to parse a given time in a specific timezone.
Given the date string "2013-01-01 00:00:00" you can convert it to the required format using the following function:
function formatDateString(string, timezone) {
return string.replace(" ", "T") + timezone;
}
Then you can create the date as follows:
new Date(formatDateString("2013-01-01 00:00:00", "+00:00"));
Another way to convert local time to GMT is as follows:
var timezone = new Date("1970-01-01 00:00:00"); // this is the start of unix time
Now that you have your own local timezone as a date object you can do:
new Date(new Date("2013-01-01 00:00:00") - timezone);
All the above methods produce the same date at GMT.
JS reads this with time zone that your computer uses.
You can try use myDate.toUTCString() for get date in UTC time.
If you want get timestamp use myDate.getTime()
Mine works simply by doing this
var datetime= new Date()
However the month is 1 low so you have to add one