I am using
<input id="123" type="date" name="date1">
I know this will use the default local time. Is there a way I could actually set it to a time with a specific time zone?
For example, my local timezone is PST but I want this input to display a time in CST time.
I have been looking for an answer for a while and still have no luck.
You can use moment.js.
Use the below function after attaching moment.js to your html file.
pass date and zone.
you can get moment.js from https://momentjs.com/
function toTimeZone(time, zone) {
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(time, format).tz(zone).format(format);
}
Related
A quick question. I have a ISO string date:
2022-07-03T10:51:09+02:00
this date as you can see has timezone included (+02:00).
Question: How to convert it into UTC date? Using e.g. date-fns or moment?
Edit: Should I just simply add "02:00" hours to current date? So it would be 12:51:09?
Trivially new Date(isoString).toISOString(), no libraries required.
const input = "2022-07-03T10:51:09+02:00";
console.log(`${input} in UTC:\n${new Date(input).toISOString()}`);
In my view, timezone is simply the representation of the same timestamp across different geographies (no. of seconds elapsed since unix time 0 is the same everywhere). So be careful while adding/removing time manually from the existing timestamp.
You can do that using moment.js like this:
var someday = moment('2022-07-03T10:51:09+02:00');
console.log(someday.utc().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment-with-locales.min.js"></script>
I have seen a question that is similar to mine (Moment.js sets dates to 1 day behind) but I can't seem to apply it.
Essentially, my date gets parsed like this:
var date = moment("2019-05-27T00:00:00Z"); // date is the 27th
When I format it to get the day, expecting the 27th, I instead receive the 26th!
date.format("DD")
Does anyone know why this might be happening and how to correct it?
http://jsfiddle.net/rmdxj26e/
You must use moment.utc(), the Moment documentation says:
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().
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time
instead of local time.
moment().format(); // 2013-02-04T10:35:24-08:00
moment.utc().format(); // 2013-02-04T18:35:24+00:00
jsFiddle Output:
Live example:
var date = moment.utc("2019-05-27T00:00:00Z");
$('#date').append($('<p>').html(date.format("DD")));
$('#date').append($('<p>').html(date.local().format("DD")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="date"></div>
The problem is the format of the parsed date. The Z letter means it is a "Zulu time" (UTC). I don't know what is your timezone, but the date is converted to your timezone.
You can parse local time format (without Z) and it should display properly.
So the full code with explanation:
var date = moment("2019-05-27T00:00:00"); // date is the 27th in local time
$('#date').append($('<p>').html(date.utc().format("DD"))); // can display 26th or 27th depends on local timezone on the PC
$('#date').append($('<p>').html(date.local().format("DD"))); // is still local so it will be 27th
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 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/
i am working on javascript. and i have time in milliseconds. so i have to display the time in PST time Zone. i have tried for GMT. but is there any option to display the time in PST format in which the input is in milliseconds like 1671673550214. so iam trying to convert this milliseconds to time and date format of PST. same as the below code does. pleas help.
Here is my tried code:
var time = new Date().getTime();
var date = new Date(time);
document.write(date.toString());
i have even tried of using UTCString(), UTC() and toString(); but things is getting converted to GMT and not to PST.
Please check this library, it has lot of functions to display and manupilate date time.
http://momentjs.com/
http://momentjs.com/docs/
Also please check following post
Moment.js: Format date in a specific timezone\
If you dont want to use external lib, see the following article
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329
You can use timezonjs which help you to convert the time to any timezone.
https://github.com/mde/timezone-js