Convert Date/Time in Gmt to Est in JavaScript

How to convert a time from GMT to EST

    try {
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone estTime = TimeZone.getTimeZone("EST");
gmtFormat.setTimeZone(estTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("EST Time: " + gmtFormat.format(sdf.parse("20140508063630")));
} catch (ParseException e) {
e.printStackTrace();
}

How to set date always to eastern time regardless of user's time zone

You can easily take care of the timezone offset by using the getTimezoneOffset() function in Javascript. For example,

var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)

dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)

var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)

Though, the locale string represented at the back will not change. The source of this answer is in this post. Hope this helps!

Is it possible to convert timestamp to date and time using GMT offset as timezone in moment.js?

UTC and GMT indicate the same time. Convert the Local time to UTC and then use utcOffset.

var m = moment(new Date()).utc().utcOffset("-06:00").format("YYYY-MM-DD HH:mm:ss z");
console.log("UTC-06:00 -> " + m);
var m = moment(new Date()).utc().utcOffset("-05:00").format("YYYY-MM-DD HH:mm:ss z");
console.log("UTC-05:00 -> " + m);
var m = moment(new Date()).utc().utcOffset("-04:00").format("YYYY-MM-DD HH:mm:ss z");
console.log("UTC-04:00 -> " + m);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Convert GMT time to local time

This worked for me:

<script>
var strDateTime = "Fri, 18 Oct 2013 11:38:23 GMT";
var myDate = new Date(strDateTime);
alert(myDate.toLocaleString());
</script>

Please take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp for all further date time manipulations, from the date object myDate.



Related Topics



Leave a reply



Submit