Javascript 从日期中删除秒/毫秒转换为 ISO 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35758963/
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
Remove Seconds/ Milliseconds from Date convert to ISO String
提问by tester123
I have a date object that I want to
我有一个我想要的日期对象
- remove the miliseconds/or set to 0
- remove the seconds/or set to 0
- Convert to ISO string
- 删除毫秒/或设置为 0
- 删除秒/或设置为 0
- 转换为 ISO 字符串
For example:
例如:
var date = new Date();
//Wed Mar 02 2016 16:54:13 GMT-0500 (EST)
var stringDate = moment(date).toISOString();
//2016-03-02T21:54:13.537Z
But what I really want in the end is
但最终我真正想要的是
stringDate = '2016-03-02T21:54:00.000Z'
回答by RobG
There is no need for a library, simply set the seconds and milliseconds to zero and use the built–in toISOStringmethod:
不需要库,只需将秒和毫秒设置为零并使用内置的toISOString方法:
var d = new Date();
d.setSeconds(0,0);
document.write(d.toISOString());
Note: toISOStringis not supported by IE 8 and lower, there is a pollyfil on MDN.
回答by jszobody
While this is easily solvable with plain javascript (see RobG's answer), I wanted to show you the momentjs solution since you tagged your questions as momentjs
:
虽然这很容易用普通的 javascript 解决(请参阅 RobG 的回答),但我想向您展示 momentjs 解决方案,因为您将问题标记为momentjs
:
moment().seconds(0).milliseconds(0).toISOString();
This gives you the current datetime, without seconds or milliseconds.
这为您提供当前日期时间,没有秒或毫秒。
Working example: http://jsbin.com/bemalapuyi/edit?html,js,output
工作示例:http: //jsbin.com/bemalapuyi/edit?html,js,output
From the docs: http://momentjs.com/docs/#/get-set/
回答by Jimmy Kane
A bit late here but now you can:
这里有点晚,但现在您可以:
var date = new Date();
var date = new Date();
this obj has:
这个 obj 有:
date.setMilliseconds(0);
date.setMilliseconds(0);
and
和
date.setSeconds(0);
date.setSeconds(0);
then call toISOString()
as you do and you will be fine.
然后toISOString()
像你一样打电话,你会没事的。
No moment or others deps.
没有任何时刻或其他人。
回答by ips
Pure javascript solutions to trim off seconds and milliseconds (that is remove, not just set to 0). JSPerfsays the second funcion is faster.
修剪秒和毫秒(即删除,而不仅仅是设置为 0)的纯 javascript 解决方案。JSPerf说第二个函数更快。
function getISOStringWithoutSecsAndMillisecs1(date) {
const dateAndTime = date.toISOString().split('T')
const time = dateAndTime[1].split(':')
return dateAndTime[0]+'T'+time[0]+':'+time[1]
}
console.log(getISOStringWithoutSecsAndMillisecs1(new Date()))
function getISOStringWithoutSecsAndMillisecs2(date) {
const dStr = date.toISOString()
return dStr.substring(0, dStr.indexOf(':', dStr.indexOf(':')+1))
}
console.log(getISOStringWithoutSecsAndMillisecs2(new Date()))
回答by Sarhanis
You can use the startOf() method within moment.js to achieve what you want.
您可以使用 moment.js 中的 startOf() 方法来实现您想要的。
Here's an example:
下面是一个例子:
var date = new Date();
var stringDateFull = moment(date).toISOString();
var stringDateMinuteStart = moment(date).startOf("minute").toISOString();
$("#fullDate").text(stringDateFull);
$("#startOfMinute").text(stringDateMinuteStart);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.js"></script>
<p>Full date: <span id="fullDate"></span></p>
<p>Date with cleared out seconds: <span id="startOfMinute"></span></p>
回答by Vishal Variyani
let date = new Date();
date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
I hope this works!!
我希望这有效!!
回答by aakarsh
We can do it using plain JS aswell but working with libraries will help you if you are working with more functionalities/checks.
我们也可以使用普通 JS 来完成,但如果您正在使用更多功能/检查,使用库会帮助您。
You can use the moment npm module and remove the milliseconds using the split Fn.
您可以使用 moment npm 模块并使用 split Fn 删除毫秒。
const moment = require('moment')
const currentDate = `${moment().toISOString().split('.')[0]}Z`;
console.log(currentDate)
Refer working example here: https://repl.it/repls/UnfinishedNormalBlock
请参阅此处的工作示例:https: //repl.it/repls/UnfinishedNormalBlock