我将时间存储在MySQL数据库中作为Unix时间戳,并将其发送到一些JavaScript代码。如何从中获取仅时间?
例如,以HH/MM/SS
格式。
我将时间存储在MySQL数据库中作为Unix时间戳,并将其发送到一些JavaScript代码。如何从中获取仅时间?
例如,以HH/MM/SS
格式。
let unix_timestamp = 1549312452;
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp * 1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
return time;
}
console.log(timeConverter(0));
var min = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes(); var sec = a.getSeconds() < 10 ? '0' + a.getSeconds() : a.getSeconds();
- user1985189getMonth()
方法返回0到11之间的月份数字,因此a.getMonth() - 1
是错误的。请修正。 - jcampbell1JavaScript操作的单位是毫秒,因此您需要先将UNIX时间戳从秒转换为毫秒。
var date = new Date(UNIX_Timestamp * 1000);
// Manipulate JavaScript Date object here...
使用:
var s = new Date(1504095567183).toLocaleDateString("en-US")
console.log(s)
// expected output "8/30/2017"
而对于时间:
var s = new Date(1504095567183).toLocaleTimeString("en-US")
console.log(s)
// expected output "3:19:27 PM"
toLocaleString
包括日期和时间。 - Sean在这个新世界中,我们应该向标准的Intl
JavaScript对象迈进,该对象具有一个方便的DateTimeFormat
构造器和一个.format()
方法:
function format_time(s) {
const dtFormat = new Intl.DateTimeFormat('en-GB', {
timeStyle: 'medium',
timeZone: 'UTC'
});
return dtFormat.format(new Date(s * 1e3));
}
console.log( format_time(12345) ); // "03:25:45"
为了与所有传统的JavaScript引擎100%兼容,以下是将秒格式化为hh:mm:ss
的最短单行解决方案:
function format_time(s) {
return new Date(s * 1e3).toISOString().slice(-13, -5);
}
console.log( format_time(12345) ); // "03:25:45"
方法
Date.prototype.toISOString()
返回的时间格式是简化扩展版ISO 8601,长度始终为24或27个字符(即YYYY-MM-DDTHH:mm:ss.sssZ
或±YYYYYY-MM-DDTHH:mm:ss.sssZ
)。时区总是零UTC偏移。
此解决方案不需要任何第三方库,并且在所有浏览器和JavaScript引擎中都受支持。
toTimeString
在处理时区方面存在问题。不幸的是,在我看到它之前,你的编辑被拒绝了。然而,我建议使用 toISOString
代替,因为 toGMTString
已经过时,并且在不同平台上可能返回不同的结果。 - VisioNnavigator.geolocation
的时间戳,两者都无法工作。 - lys我更喜欢Jacob Wright的Date.format()
库,它实现了JavaScript日期格式化,类似于PHP的date()
函数。
new Date(unix_timestamp * 1000).format('h:i:s')
将秒数格式化为hh:mm:ss的最短一行解决方案:
console.log(new Date(1549312452 * 1000).toISOString().slice(0, 19).replace('T', ' '));
// "2019-02-04 20:34:12"
我建议使用像momentjs.com这样的库,它可以使此过程变得非常简单:
基于Unix时间戳:
var timestamp = moment.unix(1293683278);
console.log( timestamp.format("HH/mm/ss") );
根据一个 MySQL 的日期字符串:
var now = moment("2010-10-10 12:03:15");
console.log( now.format("HH/mm/ss") );
console.log
示例中,我因为 /
的格式而感到困惑,但这对我帮助很大。 - NetOperator Wibby现在你需要使用Unix时间戳:
const dateTimeString = moment.unix(1466760005).format("DD-MM-YYYY HH:mm:ss");
这适用于PHP时间戳
var d = 1541415288860;
//var d =val.timestamp;
//NB: use + before variable name
var date = new Date(+d);
console.log(d);
console.log(date.toDateString());
console.log(date.getFullYear());
console.log(date.getMinutes());
console.log(date.getSeconds());
console.log(date.getHours());
console.log(date.toLocaleTimeString());
var d =val.timestamp;
var date=new Date(+d); //NB: use + before variable name
console.log(d);
console.log(date.toDateString());
console.log(date.getFullYear());
console.log(date.getMinutes());
console.log(date.getSeconds());
console.log(date.getHours());
console.log(date.toLocaleTimeString());
上述方法将生成此结果。
1541415288860
Mon Nov 05 2018
2018
54
48
13
1:54:48 PM
有很多方法可以完美地使用时间戳,无法列举所有。