My server returns date in CEST time zone. Is there any way to convert that date to local user date? Something like
var CEST_date = new Date( some_CEST_timestamp );
var local_time = //some code that converts CEST_date to local user date.
I've found Moment.js library but I cannot find timezone to different timezone conversion in the docs.
Related
In my file HomeComponent.ts (not in template html). I create a new Date and show it in console like this:
var fecha = new Date();
console.log(fecha);
the time in my country now is 16:09 (UTC -3) but the console output shows the date in UTC:
Date 2018-12-20T19:09:32.910Z // the time is 19:09
I need to compare and do some operations with "this new date" and other dates saved in a DB so I need the new Date to be created in my local timezone. How can I create a new Date in my local timezone?
How can I create a new Date in my local timezone?
Dates don't have a timezone, they are simply an offset from 1970-01-01T00:00:00Z (a time value) so are effectively always UTC. They represent a particular moment in time and can be used to generate a string representing an equivalent date and time in any timezone.
The local offset comes from the host system, it's used (if necessary) when creating a date and when working with local date and time values. There are equivalent UTC methods for doing operations that don't consider the local timezone.
The default toString method will generate a timestamp for the host timezone, toISOString will use UTC, toLocaleString can be used to generate a timestamp for any timezone. All will represent the same UTC date and time, just in different timezones.
When comparing dates, it's the UTC time value that is compared as it provides a common factor for all dates.
the time in my country now is 16:09 (utc -3) but the console output show the date in utc
A Date or DateTime is a structure, it does not have a format. If you want to display a formatted date string using the timezone of the browser then call toLocaleString.
var fecha = new Date();
console.log("As ISO8601 in utc:", fecha);
console.log("As local:", fecha.toLocaleString());
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)
Is it possible for Momentjs to display the localtime and autoformat the string.
I have eg. the time 2015-03-20T09:08:53+01:00 and want momentjs to display in local time and format this in danish starting DD-MM-YYYY
$('[data-momentdate]').each(function () {
var localTime = moment.utc($(this).attr('data-momentdate')).toDate();
localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
$(this).html(localTime);
});
The above code converts the utc time to local danish time, but I want momentjs to determine which format to use based by the timezone
Any ideas ?
Lets say you have a UTC date-time string as 2014-02-19 05:24:32 AM and you want to determine time in your timezone then use following code:
moment.utc('2014-02-19 05:24:32 AM').toDate();
toDate() method gives javascript Date() object.
$(function(){
setInterval(function(){
var divUtc = $('#divUTC');
var divLocal = $('#divLocal');
//put UTC time into divUTC
divUtc.text(moment.utc().format('YYYY-MM-DD HH:mm:ss'));
//get text from divUTC and conver to local timezone
var localTime = moment.utc(divUtc.text()).toDate();
localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
divLocal.text(localTime);
},1000);
});
For more information look at: How to get local time from UTC using Moment.JS.
Update:
By default, moment parses and displays in local time. If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
moment().format(); // 2013-02-04T10:35:24-08:00
moment.utc().format(); // 2013-02-04T18:35:24+00:00
On the other hand, there maybe different format for one timezone and in that case you should give the format. In addition to this, if you want to use the same format you can use globalization property as below on the web.config:
<system.web>
<globalization culture="de-DE" uiCulture="de-DE" />
i have to convert date in utc from locale date time in birt.
The only problem is that the date is divide in two numeric data type like '20131012' instead for 'yyyyMMdd' and '223112'instead for 'h24:mi:ss'.
Can anyone help to convert this two data type affected from locale settings, with other two in UTC mode?
thanks for anyone just read this..
Javascript Date objects are based on a UTC time value. When methods such as date.toString are called, the local system settings are used to show a local date and time.
You can use Date.UTC to create a UTC time value, use that to create a date object, then use the date object to get a local (system) equivalent date and time.
e.g.:
var utcDate = '20131012';
var utcTime = '223112';
// Get a UTC time value
var timeValue = Date.UTC(utcDate.substring(0,4),
utcDate.substring(4,6) - 1, // Months are zero indexed
utcDate.substring(6),
utcTime.substring(0,2),
utcTime.substring(2,4),
utcTime.substring(4)
);
// Convert time value to date object
var date = new Date(timeValue);
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 });