Reputation: 551
I am getting date info as number in milliseconds. I am able to parse it properly. But when I change my pc timezone the parsed time is also changed. I want only to a particular timezone only.
EX.
Time in millisec: 1461645900000
Actual Parsed: 26-Apr-2016 10:15:00 IST
when pc timezone is changed it become: Tue Apr 26 2016 14:15:00 GMT+0930 (Local Standard Time)
Here I want only the actual time. How to get it?
Here is My little try:
function parseDateTime(dt, format) {
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" ];
var ret = "";
if (dt) {
var day="",month="",year="",hour="",mins="",secs="";
var d = new Date(dt)
var os=d.getTimezoneOffset();
if(os<0){
}
ret += d.getDate() + "-" + months[parseInt(d.getMonth())] + "-"
+ d.getFullYear() + " " + leftPad(d.getHours(), 2, "0") + ":"
+ leftPad(d.getMinutes(), 2, "0") + ":"
+ leftPad(d.getSeconds(), 2, "0");
}
document.getElementById("d1").innerHTML=ret;
console.log(ret);
}
function leftPad(number, targetLength, withWhat) {
var output = number + '';
while (output.length < targetLength) {
output = withWhat + output;
}
return output;
}
parseDateTime(1461645900000);
I have used new Date().toLocaleString("en-US", {timeZone: ""}) but limited. Also I am not interested in new lib.
Upvotes: 4
Views: 31005
Reputation: 5797
var aestTime = new Date().toLocaleString("en-US", {timeZone: "Australia/Brisbane"});
aestTime = new Date(aestTime);
console.log('AEST time: '+aestTime.toLocaleString())
var asiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Shanghai"});
asiaTime = new Date(asiaTime);
console.log('Asia time: '+asiaTime.toLocaleString())
var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
usaTime = new Date(usaTime);
console.log('USA time: '+usaTime.toLocaleString())
first create the date object(new Date()) of YOUR date and convert.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
Upvotes: 3
Reputation: 1927
I understand that you wanted to get a same time irrespective of time zone when ever you pass the milliseconds.
instead of the following two lines
var d = new Date(dt)
var os = d.getTimezoneOffset();
you include the below lines
var os = new Date()
os = os.getTimezoneOffset();
var d = new Date((dt + (os * 60 * 1000)));
This adjusts your milliseconds to UTC time based on your local timezone and creates the date object from the adjusted UTC time, so that you will always get same time, irrespective of your local time zone.
Upvotes: 6
Reputation:
You're confused about the difference between times and representations of times. There is only a single time 1461645900000. It's the same everywhere in the universe. It's a magic moment where the stars are aligned in a certain way and the universe is at a special point in its progression.
Timezones are a function of representing a date. If I live in Tokyo, I am going to probably prefer seeing my times in Tokyo time. If I live in California, I am going to probably prefer seeing my times in California time. Such local time representations consist of a local time and an offset from UTC, so it can be properly interpreted.
When you print or output a date in JS, and it is turned into a string as it often is, of course, that string, being a representation, has to be relative to somewhere on the globe--in other words, relative to some offset. The default JS behavior is to use the offset where are you located right now, as set on your local computer. That's why you will see something like "Tue Apr 26 2016 10:15:00 GMT+0530 (India Standard Time)" if you computer is set to Indian time. You could also try date.toISOString
which would give you "2016-04-26T04:45:00.000Z" (the "Z" means UTC), or date.toUTCString
, which would ignore your local timezone and give the time relative to UTC, as in "Tue, 26 Apr 2016 04:45:00 GMT".
Just for completeness, people quite commonly confuse offsets and timezones. A timezone is a complete historical record of all offsets (which can change, such as in the case of DST) over time for a particular region. There are actually hundreds of different timezones, including one for some town in Indiana that went off DST in 1929. If you want to manage timezone data, you are going to have to handle that separately from the time itself. Even if you happen to have an offset, there is no way to go uniquely from an offset to a timezone--for instance, California and Chile are different timezones but yet are both offset -08:00. To manage timezones properly, you are going to have to use some kind of special timezone package.
I often hear people saying they "want to change the timezone". You can't change a time's timezone. Times themselves don't have timezones, they are just times on a cosmic clock. What you can do is to change the representation of a time so as to be relative to a particular offset. For example, you can use date.toLocaleString
as you have tried. What did you find to be "limited" about it?
> new Date().toLocaleString("en-US", {timeZone: "America/Los_Angeles", timeZoneName: "long"})
< "11/3/2016, 8:33:02 AM Pacific Daylight Time"
You can also use timeZoneName: "short"
, which will yield "PDT". However, "PDT" or "PST" or "IST" or anything else cannot be used as the value of the timeZone
option, because these three-letter abbreviations are not unique. For instance, "PST" could also mean "Pitcairn Standard Time".
The format you tried, giving timeZone
as a null string, will not work (yields an unidentified timezone error).
What if you want to give a string such as "IST"? As mentioned above, this will not work; you will have to find a way to map "IST" to "Asia/Kolkata", which will require some kind of library. What if you want to specify some kind of offset? Here again, you will need some kind of library which can infer a timezone from an offset.
The bottom line is that if you find yourself parsing the date, adjusting this or that component, and piecing it back together with string arithmetic, you are doing something either wrong or unnecessary.
Upvotes: 14
Reputation: 147343
Given:
Time in millisec: 1461645900000
If you create a date using:
new Date(1461645900000);
then the value is treated as a millisecond offset from 1970-01-01T00:00:00Z and represents the same moment in time regardless of the host system settings. When you write this as a string:
Actual Parsed: 26-Apr-2016 10:15:00 IST
then the host system time zone settings are used to create "local" date and time values from the UTC time value. IST is UTC+05:30, so the equivalent UTC date is:
2016-04-26T04:45:00Z
If you change your PC time zone to one that has a different offset, then of course you'll see a different "local" time, but the internal time value of the date hasn't changed, nor has the moment in time that it represents. So when you change to an offset of UTC+09:30, the "local" time is 4 hours later, and you get:
Tue Apr 26 2016 14:15:00 GMT+0930 (Local Standard Time)
So when you say:
Here I want only the actual time. How to get it?
That is the actual time in that time zone for that moment in time, i.e.
26-Apr-2016 10:15:00 IST (UTC+0530)
Tue Apr 26 2016 14:15:00 GMT+0930
2016-04-26T04:45:00Z
are all different representations of the same moment in time.
If you want to represent any date in a particular time zone, you can use UTC methods to adjust the UTC time to the offset required, then use UTC methods to get the date and time values to display.
Upvotes: 1