JavaScript : How Do I Convert UTC Date Time To EST Hours Using Pure Javascript?
Solution 1:
You should not parse strings with the Date constructor, it's unreliable and largely implementation dependent. IE 8 won't parse ISO 8601 like strings.
A string like "2016-04-10 9:00:00.0" can be easily parsed by getting the numbers and passing them to the Date constructor. To then apply an offset is also straight forward (what you're doing is OK but it can be included in the parse function if you like), see function below. What works in IE 8 will also work everywhere else.
I've assumed that your offset is ±h.mm but it may be decimal hours, in that case just modify the offset part (or apply it separately). I apply the offset as minutes directly to the minutes value rather than the time value.
However, any such functions should validate the values and return appropriate error messages if they fail.
// Date string like: 2016-04-10 09:00:00.0
// Offset like: -5.00 assuming h.mm
function parseString(dateString, offset) {
var b = dateString.split(/\D+/);
var o = (offset < 0? -1 : 1) *
(Math.abs(parseInt(offset)*60) + +(offset.replace(/^.*\.(\d+)$/,'$1')));
return new Date(Date.UTC(b[0], b[1]-1, b[2], b[3], +b[4]-o,
b[5], (b[6]+'00').slice(0,3)));
}
// Sample values
var dateString = '2016-04-10 09:00:00.0'
var offset = '-5.00';
var d = parseString(dateString, offset);
document.write(
'<br>Time : ' + dateString +
'<br>Offset: ' + offset +
'<br>Local : ' + d);
body {
font-family: courier, monospace;
white-space: pre;
}
Solution 2:
If this is NOT homework, immediately delete all your custom date handling code, and use a library. Install moment.js and moment-timezone.js, and your code will become something like:
moment.tz.add("America/New_York|EST EDT EWT EPT|50 40 40 40|01010101010101010101010101010101010101010101010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261t0 1nX0 11B0 1nX0 11B0 1qL0 1a10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x40 iv0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0|21e6");
console.log(moment.tz(utcTimeString, "America/New_York").format());
Post a Comment for "JavaScript : How Do I Convert UTC Date Time To EST Hours Using Pure Javascript?"