/    Sign up×
Community /Pin to ProfileBookmark

Converting date in millisecond to dd-mmm-yyy hh:mm:ss pm/am timezone format

Hello,
We have servers in USA. But clients are in different time zones (including foreign countries). So when a client logs in, the server saves the last login time in a ORACLE/SQL database. Now when the client logs in again we want to show the client the last login time in his time zone.

I can get the Date object from the server in milliseconds to javascript. But How to convert that milliseconds to client’s time zone and display a sting like : 26-Sep-2017 8:48:34 AM CST.

Now if this string to be shown in a different country, it must be shown in that country’s time and zone.
-devmp

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rootSep 26.2017 — I assume you are using PHP as the backend?

Use PHP to generate a time string from the data serverside, you can output it for display or use it in a JavaScript Date function to track time...

That leads to the next question is what you want to do with that date string?
Copy linkTweet thisAlerts:
@mpdevkpauthorSep 26.2017 — I assume you are using PHP as the backend?

Use PHP to generate a time string from the data serverside, you can output it for display or use it in a JavaScript Date function to track time...

That leads to the next question is what you want to do with that date string?[/QUOTE]


Is there a way to do this without PHP, just plain ole javascript, jquery? On the server side we use Java/Beans/PrimeFaces. Or any other method? Also the final string is simply displayed to the user when the user logs in.
Copy linkTweet thisAlerts:
@rootSep 26.2017 — Whatever your server uses for a server-side scripting, PHP is the most commonly used method, so I can only show you a simple PHP method.

Untested, but goes something like this...
[code=php]date_default_timezone_set("Europe/London"); // insert the timezone reference for your server or the time zone you want to use for your times.
$serverTime = date("r"); // generates an ISO date string.
?>
<script>
Date.prototype.getDDMMYY = function( ){

var dd = this.getDate();
var mm = 1 + this.getMonth();
var yy = this.getFullYear();

dd = "00".slice( (""+dd).length ) + dd; // pad
mm = "00".slice( (""+mm).length ) + mm; // pad

return [dd,mm,yy].join("/");
}
theDateObject = new Date("<?php echo $serverTime; ?>"); // the date reference used for calculation of the time
dateNow = theDateObject.getDDMMYY();
timeNow = theDateObject.toUTCString().slice(17,25); // gets 00:00:00 format time
lastLogin = [dateNow,timeNow].join(" ");
// output to console to check output.
console.log("Last Login : %s ", lastLogin );
</script>
[/code]
×

Success!

Help @mpdevkp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...