javascript 从日期字符串中获取时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15918765/
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-27 02:35:30  来源:igfitidea点击:

Get the time from a date string

javascriptdatetime

提问by vishnu

How do I get the time from the date string using JavaScript.

如何使用 JavaScript 从日期字符串中获取时间。

My datestring is in the following way: 2013-04-08T10:28:43Z

我的日期字符串如下:2013-04-08T10:28:43Z

How to split the time in hours and minutes format. I want to show the activity stream in the following way:

如何以小时和分钟格式分割时间。我想通过以下方式显示活动流:

xxx has updated 2hrs ago
yyy has updated 3min ago

xxx 2 小时前
更新 yyy 3 分钟前更新

回答by Stasel

Just create new Date object:

只需创建新的 Date 对象:

var myDate = new Date("2013-04-08T10:28:43Z");

var minutes = myDate.getMinutes();
var hours = myDate.getHours();

回答by lib3d

Extract a date from your dateString

从 dateString 中提取日期

First extract the numbers with

首先提取数字

var sp = dateString.match(/\d+/g)

Then you can build a Date object or skip this step

然后你可以构建一个Date对象或者跳过这一步

var dateObject = new Date(+sp[0], +sp[1]-1, +sp[2], +sp[3], +sp[4], +sp[5])

And call getHoursand getMinuteson this Date object.

并在此 Date 对象上调用getHoursand getMinutes

If you skip this then directly get +sp[3]for hours and +sp[4]for minutes.


Compute the difference

如果您跳过此步骤,则直接获取+sp[3]数小时和+sp[4]数分钟。


计算差异

Since you seem to have to compare with now, you will get time difference this way:

由于您似乎必须与现在进行比较,因此您会以这种方式获得时差:

var timeDifference = new Date(new Date - dateObject);

And then call getHoursand getMinuteson timeDifference.

然后调用getHoursgetMinutes继续timeDifference

回答by mvladk

Simple Javascript is more than enough: Date.parse will convert your string to timestamp:

简单的 Javascript 就足够了:Date.parse 会将您的字符串转换为时间戳:

var date_string = '2013-04-08T10:28:43Z';

var your_date_object = new Date();
your_date_object.setTime(Date.parse( date_string ));

var min = your_date_object.getUTCMinutes();
var hour = your_date_object.getUTCHours();

回答by hbhakhra

This is whats known as an ISO string. There are utilities to parse ISO strings as regular JavaScript dates out there such as Dojo.

这就是所谓的 ISO 字符串。有一些实用程序可以将 ISO 字符串解析为常规 JavaScript 日期,例如 Dojo。

var date = dojo.date.stamp.fromISOString("2013-04-08T10:28:43Z");

http://dojotoolkit.org/reference-guide/1.8/dojo/date/stamp.html

http://dojotoolkit.org/reference-guide/1.8/dojo/date/stamp.html

回答by Bogdan Haidu

var curr_date = new Date();
var test_date = new Date("2016-01-08 10:55:43");

hours_diff=Math.abs(test_date.getHours()-curr_date.getHours());
minutes_diff=Math.abs(test_date.getHours()*60+test_date.getMinutes()-curr_date.getHours()*60-curr_date.getMinutes());
console.log("xxx has updated "+hours_diff+" hours ago");
console.log("xxx has updated "+minutes_diff+" minutes ago"); //not so beautiful

///STILL IF THE date is a string and !!NOT CREATED with DATE 
/// "Get the time from a date string" might have this solution

var date = "2016-01-08 10:55:43";
var hours = date.slice(-8);

console.log("hours and minutes string is "+hours);

fiddle test

小提琴测试