如何使用 jquery/javascript 格式化时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4725975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:41:28  来源:igfitidea点击:

how to format time with jquery/javascript

javascriptjquery

提问by meijiOrO

I have a JSON feed that drupal spits out time in this format: 2010-12-16T04:41:35Z

我有一个 JSON 提要,drupal 以这种格式吐出时间: 2010-12-16T04:41:35Z

How do I go about formating it to X minutes/hours ago?

我如何将其格式化为 X 分钟/小时前?

回答by David Glenn

Take a look at the timeago jQuery pluginwhich can be used programtically like

看一下timeago jQuery 插件,它可以像编程一样使用

var t = jQuery.timeago("2010-12-16T04:41:35Z"); 

or on HTML elements like

或在 HTML 元素上,例如

<time class="timeago" datetime="2008-07-17T09:24:17Z">July 17, 2008</time>
<script type="javascript">
  jQuery(document).ready(function() {
    jQuery("time.timeago").timeago();
  });
</script>

回答by Slick23

I think something on this page will help you. There's a couple formatting jQuery plugins toward the bottom. Good luck!

我认为此页面上的某些内容会对您有所帮助。底部有几个格式化 jQuery 插件。祝你好运!

回答by Kyle Wild

回答by alfred

does the 04:41:35 part is correct? or do you need like to take into consideration time zones and stuff?

04:41:35 部分是否正确?或者你需要考虑时区和东西吗?

because basically this can work:

因为基本上这可以工作:

var str = "2010-12-16T04:41:35Z"
var arr = str.split("T")
arr[0] = arr[0].split("-")
arr[1] = (arr[1].replace(/Z/ig,"")).split(":")
var year = arr[0][0]
var month = parseInt(arr[0][1],10)-1
var day = arr[0][2]
var hours = arr[1][0]
var minutes = arr[1][1]
var seconds = arr[1][2]
var d = new Date(year, month, day, hours, minutes, seconds, 0);
var now = (new Date())
var diffMS = now.getTime() - d.getTime()
//alert ("difference in milliseconds: " + diffMS)

var hoursago = diffMS/1000/60
var ANDminutes = (diffMS - Math.floor(hoursago) * 1000*60)/1000

alert (Math.floor(hoursago) + " hours and " + ANDminutes + " minutes ago")

回答by Will Tomlins

<shameless_plug>

Here's a library which give you a humanized time difference.

这是一个图书馆,可以为您提供人性化的时差。

https://github.com/layam/js_humanized_time_span

https://github.com/layam/js_humanized_time_span

The output is completely customizable, allowing you to set different output formats for various time bands; So you could have:

输出是完全可定制的,允许您为不同的时间段设置不同的输出格式;所以你可以有:

'10 seconds ago'

or

或者

'3 days and 2 hours ago'

you can even set custom time units.

您甚至可以设置自定义时间单位。

Additionally it's native JS and so doesn't rely on any framework.

此外,它是原生 JS,因此不依赖于任何框架。

</shameless_plug>

回答by Fran Verona

You can use regexp objectin JS to build your own regular expression, separate hours-minutes-secs from the original string and then do whatever you want.

您可以在 JS 中使用regexp 对象来构建您自己的正则表达式,将小时-分钟-秒与原始字符串分开,然后做任何您想做的事情。

Tip: actual_date - your original string time

提示: actual_date - 您的原始字符串时间