/    Sign up×
Community /Pin to ProfileBookmark

Mysql Stamp to JS date?

How do i change timestamp from a MYSQL database like this “1164279208” into a JS date 07-11-2007 ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisAug 08.2007 — What you have posted appears to be a Unix timestamp rather than a MySQL timestamp. Unix timestamps are the # of seconds since 1/1/1970. MySQL timestamps are yyyymmddhhmmss.

JavaScript uses milliseconds since 1/1/1970. So if you actually have a Unix timestamp, just multiply it by 1000 and convert it using new Date().
<i>
</i>var unixTime = 1164279208;
var jsTime = unixTime * 10000;
var theTime = new Date(jsTime);
alert(theTime); // you will get "Thu Nov 23 05:53:28 EST 2006"
var theMonth = theTime.getMonth() + 1;
theMonth = (theMonth &lt; 10) ? "0" + theMonth : theMonth;
var theDay = theTime.getDay();
theDay = (theDay &lt; 10) ? "0" + theDay : theDay;
var theYear = theTime.getFullYear();
alert(theMonth + "-" + theDay + "-" + theYear); // will be "11-23-2006"

If you actually have a MySQL timestamp:
<i>
</i>var MySQLTime = 20061123000000;
var theMonth = Math.floor((MySQLTime - theYear * 10000000000) / 1000000);
var theDay = theMonth % 100;
theMonth = Math.floor(theMonth / 100);
theMonth = (theMonth &lt; 10) ? "0" + theMonth : theMonth;
theDay = (theDay &lt; 10) ? "0" + theDay : theDay;
alert(theMonth + "-" + theDay + "-" + theYear); // will be "11-23-2006"
Copy linkTweet thisAlerts:
@kreelinauthorAug 08.2007 — Thank you, this is wht i did
[CODE]function dateTransform (smartlinkdate)
{
var ladate = new Date(smartlinkdate*1000);
var lejour = ladate.getDay();
var lemois = ladate.getMonth();
var lannee = ladate.getFullYear();
return (lejour + "-" + lemois + "-" + lannee);
}[/CODE]
×

Success!

Help @kreelin 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...