Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  190] [ 7]  / answers: 1 / hits: 27163  / 7 Years ago, fri, may 19, 2017, 12:00:00

To convert epoch dateTime to human readable , using a simple new date(1495159447834) will suffice.



The problem I'm encountering now is that for my hybrid application, if the user set the time-zone in his phone date time setting to lets say GMT +12:00 ,the human readable dateTime will be different from what I would want the user to have and I would want him/her to follow the server timezone.



Thus , how would I convert the epoch number to a specific given timezone in a human readable format.



I have tried example like:



var test= new Date('1495159447834 GMT+0800').toString();


and it returns me an Invalid Date.



If possible, I would want this without any libraries. I have looked through the answers here and I believe that I could not find any answers I'm looking for. If there is any previously answered question with the same topic, do let me know and I will close this question!


More From » datetime

 Answers
24

You can use offset to convert your current datetime to a specific timezone.


function convertEpochToSpecificTimezone(timeEpoch, offset){
var d = new Date(timeEpoch);
var utc = d.getTime() + (d.getTimezoneOffset() * 60000); //This converts to UTC 00:00
var nd = new Date(utc + (3600000*offset));
return nd.toLocaleString();
}
// convertEpochToSpecificTimezone(1495159447834, +3)

The offset will be your specific timezone. Example: GMT +03:00, your offset is +3. If GMT -10:00, offset is -10


[#57734] Wednesday, May 17, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronaldo

Total Points: 694
Total Questions: 85
Total Answers: 103

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
;