JavaScript unixtime problem
I get the time from the database in Unix format.
It looks like this: console.log (time); Result: 1300709088000
Now I want to reformat it and pick out only the time, I found this: Convert a Unix timestamp to time in JavaScript
That did not work as I want. The time I get is this:
1300709088000
9:0:0
1300709252000
6:33:20开发者_运维知识库
1300709316000
0:20:0
1300709358000
12:0:0
1300709530000
11:46:40
It is very wrong times when I know that times are quite different. How can I fix it?
console.log(time);
var date = new Date(time*1000);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
// will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes + ':' + seconds;
console.log(formattedTime);
It looks like this: console.log (time); Result: 1300709088000
That doesn't look like a Unix timestamp (seconds since The Epoch), it looks like milliseconds since The Epoch. So you wouldn't multiply by 1000 to convert from seconds to milliseconds for JavaScript, it's already in milliseconds (or you're dealing with dates more than 41,000 years from now; which is fair enough).
Test:
var times = [
1300709088000,
1300709252000,
1300709316000,
1300709358000,
1300709530000
];
var index;
for (index = 0; index < times.length; ++index) {
display(times[index] + " => " + new Date(times[index]));
}
Live copy
Update: Or getting the individual parts:
var times = [
1300709088000,
1300709252000,
1300709316000,
1300709358000,
1300709530000
];
var index, dt;
for (index = 0; index < times.length; ++index) {
dt = new Date(times[index]);
display(times[index] +
" => " +
dt +
" (" + formatISOLikeDate(dt) + ")");
}
// Not all implementations have ISO-8601 stuff yet, do it manually
function formatISOLikeDate(dt) {
var day = String(dt.getDate()),
month = String(dt.getMonth() + 1), // Starts at 0
year = String(dt.getFullYear()),
hour = String(dt.getHours()),
minute = String(dt.getMinutes()),
second = String(dt.getSeconds());
return zeroPad(year, 4) + "-" +
zeroPad(month, 2) + "-" +
zeroPad(day, 2) + " " +
zeroPad(hour, 2) + ":" +
zeroPad(minute, 2) + ":" +
zeroPad(second, 2);
}
function zeroPad(str, width) {
while (str.length < width) {
str = "0" + str;
}
return str;
}
Live copy ...but if you're going to be doing much of anything with dates, I'd look at DateJS.
Your time stamps are not in Unix format, they're already in the Javascript millisecond resolution format.
Hence you shouldn't be multiplying by 1000 when you create your Date
object.
I've tried to do something like this:
console.log (time);
where date = new Date (time);
/ / hours party from the timestamp
was hours = date.getHours ();
/ / party minutes from the timestamp
Every minute = date.getMinutes ();
/ / Seconds Party From The timestamp
where seconds = date.getSeconds ();
/ / Will display time up 10:30:23 format
was formattedTime = hours + ':' + minutes + ':' + seconds;
console.log (formattedTime);
The result is this: 1300709088000 NaN: NaN: NaN
精彩评论