我有这个日期&小时制格式2016-03-07 15:13:49
。我想按照1分钟前,1小时前或1年前的时间显示,具体取决于从现在开始的日期。将yyyy-mm-dd hh:mm:ss格式转换为时间前
1
A
回答
1
您需要将日期格式转换为js date
对象,然后你可以使用timeSince
功能从this答案
var date = new Date('2016-03-07T15:13:49')
document.write("js date: " + date + "<br><br>");
document.write("timesince: ");
document.write(timeSince(date));
function timeSince(date) {
var seconds = Math.floor((new Date() - date)/1000);
var interval = Math.floor(seconds/31536000);
if (interval > 1) {
return interval + " years";
}
interval = Math.floor(seconds/2592000);
if (interval > 1) {
return interval + " months";
}
interval = Math.floor(seconds/86400);
if (interval > 1) {
return interval + " days";
}
interval = Math.floor(seconds/3600);
if (interval > 1) {
return interval + " hours";
}
interval = Math.floor(seconds/60);
if (interval > 1) {
return interval + " minutes";
}
return Math.floor(seconds) + " seconds";
}
+0
谢谢,这将是解决方案。 –
+0
不客气:) –
0
var past_date = new Date('2016-07-28T05:13:49'); // the date will come here
var time_diff = new Date()- past_date; // getting the difference between the past date and the current date
var min = Math.floor(time_diff/60000); // Converting time in to minutes
var seconds = 59,
minutes = Math.floor(min%60),
hours = Math.floor(min/60);
if(hours > 24){ // Checking if the hours ids more than 24 to display as a day
var days = hours/24;
days = days.toFixed(0);
document.write("last updated:" + days + " days ago");
}else if(hours > 1){ // if time is less than the 24 hours it will display in hours
document.write("last updated:" + hours + " hours ago");
}else{
document.write("last updated:" + minutes + " minutes ago");
}
2
如果你不关心的准确性,我认为moment是一个更好的方法。
例如:
var m = require('moment');
m("2016-03-07 15:13:49","YYYY-MM-DD HH:mm:ss").fromNow(); // 5 months ago
m("2016-07-28 12:13:49","YYYY-MM-DD HH:mm:ss").fromNow(); // 2 hours ago
m("2016-07-28 13:13:49","YYYY-MM-DD HH:mm:ss").fromNow(); // 36 minutes ago
m("2016-07-28 13:49:00","YYYY-MM-DD HH:mm:ss").fromNow(); // a minute ago
m("2016-07-28 13:50:00","YYYY-MM-DD HH:mm:ss").fromNow(); // a few seconds ago
相关问题
- 1. 将系统日期时间转换为特定格式。例如:(yyyyMMdd hh:MM:ss)
- 2. 转换器 - >将日期时间转换为HH:mm:ss格式
- 3. 程序将时间以秒为单位转换为hh:mm:ss格式
- 4. 将yyyymmdd转换为月份格式
- 5. 将YYYYMMDD转换为Unix中的时间
- 6. 将HH:MM:SS格式的时间转换为仅秒?
- 7. 将时间(hh:mm:ss)转换为R
- 8. 转换HH:MM:SS时间格式为在谷歌电子表格
- 9. 有没有办法将时间从“hh:mm:ss”格式转换为“hh:mm AM/PM”格式?
- 10. 转换格式为HH:MM AM/PM至HH:MM:SS
- 11. 将小时,分钟和秒转换为HH:MM:SS格式
- 12. XSL将时间值转换为“分钟/小时/天前”格式
- 13. 转换日期时间以字符串格式YYYYMMDD
- 14. 转换日期/时间YYYYMMDD/HHMMSS格式到Python的datetime
- 15. 将Varchar转换为时间格式
- 16. 将整数转换为时间格式
- 17. 将facebook时间转换为GMT格式?
- 18. 转换为时间格式
- 19. 已用时间格式HH:mm:ss
- 20. 输出时间格式为“hh:mm:ss”vb
- 21. 转换Unix纪元时间格式HH:MM:SS没有日期
- 22. 转换(YYYY-MM-DD-HH:MM:SS)日期时间
- 23. 转换时间以hh:mm:ss AM(PM)格式(sql)
- 24. 将当前时间戳转换为DD MMM YYYY格式
- 25. 使用yyyyMMdd格式化将字符串转换为DateTime
- 26. 在pyspark中将yyyymmdd转换为MM-dd-yyyy格式
- 27. 在MDX中将“YYYYMMDD”格式字符串转换为日期?
- 28. 在R中将YYYYMMDD转换为mm/dd/yyyy格式
- 29. 将hh:mm:ss转换为秒
- 30. SQL日期格式转换YYYYMMDD
@RamanSahasi中重复的问题的答案,是它适用于日期格式我有吗? –
你只需要转换你的时间格式。查看我的答案并运行代码片段。 –