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
Input Date Format With Dd/Mm/Yyyy in the Input Field
How to Reload a Page With React-Router
React | How to Detect Page Refresh (F5)
How to Download File in React Js
How to Get Only 1St Element of Json Data
Regex to Allow Only Certain Special Characters and Restrict Underscore
How to Restrict Past Dates in Html5 Input Type Date
Loading Screen Gets Stuck While Processing Http Request
Disable/Hide Download Button in Iframe on Default Pdf Viewer
How to Refresh Form Page After Post Request
How to Reload Datatables from Another Ajax Request With Onclick
How to Close Toggle Menu When Clicking Navigation Item Anchor Link
Javascript String With New Line - But Not Using \N
Horizontal Scrollbar on Top and Bottom of Table
Webpack: Module Not Found: Error: Can't Resolve (With Relative Path)
Split a String Only the At the First N Occurrences of a Delimiter
How to Add a New Object (Key-Value Pair) to an Array in JavaScript