Javascript 从没有时区的日期获取 ISO 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43670062/
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
Get ISO string from a date without time zone
提问by Maximus Decimus
My configuration is Lisbon time zone. When I do new Date()I get my current local date/time which is Fri Apr 28 2017 01:10:55 GMT+0100 (GMT Daylight Time)
我的配置是里斯本时区。当我这样做时,new Date()我会得到我当前的本地日期/时间Fri Apr 28 2017 01:10:55 GMT+0100 (GMT Daylight Time)
When I get the ISO string with toISOString()it will apply the time zone and I will get:
当我得到 ISO 字符串时toISOString(),它将应用时区,我将得到:
2017-04-28T00:10:55.964Z
The problem is that some minutes ago the date time was like this (yesterday):
问题是几分钟前的日期时间是这样的(昨天):
2017-04-27T23:45:05.654Z
I tried moment.js (new to this) and I did something like this
我试过moment.js(新来的),我做了这样的事情
document.write(moment('2017-04-28').format())
But I get this 2017-04-28T00:00:00+01:00which is not 2017-04-28T00:00:00.000Z
但我明白这2017-04-28T00:00:00+01:00不是2017-04-28T00:00:00.000Z
I want to pass this value as a parameter of a restful method to parse it automatically as a DateTime type, but if I pass the output from moment.js format then it will get parsed as 2017-04-27 23:00:00.00
我想将此值作为restful方法的参数传递以将其自动解析为DateTime类型,但是如果我从moment.js格式传递输出,那么它将被解析为 2017-04-27 23:00:00.00
If I create a new date with new Date()or with new Date('2017-04-27')(date portion), I just want to get the ISO format like as follows with no time with no time zone
如果我创建一个带有new Date()或带有new Date('2017-04-27')(日期部分)的新日期,我只想获得如下的 ISO 格式,没有时间没有时区
2017-04-28T00:00:00.000Z
Is there a javascript method like toISOString() or using maybe moment to get that format?
是否有像 toISOString() 这样的 javascript 方法或使用 moment 来获取该格式?
No matter what time zone or moment of day, I just to simulate that is midnight of the date given.
无论是哪个时区或一天中的哪个时刻,我只是模拟给定日期的午夜。
回答by RobG
It's very unclear what you're asking. If you want the UTC date with the hours always 0, then set the UTC hours to 0 and use toISOString, e.g.
很不清楚你在问什么。如果您希望 UTC 日期的小时数始终为 0,则将 UTC 小时数设置为 0 并使用toISOString,例如
var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());
Of course this is going to show the UTC date, which may be different to the date on the system that generated the Date.
当然,这将显示 UTC 日期,该日期可能与生成日期的系统上的日期不同。
Also,
还,
new Date('2017-04-27').toISOString();
shouldreturn 2017-04-27T00:00:00Z (i.e. it should be parsed as UTC according to ECMA-262, which is contrary to ISO 8601 which would treat it as local), however that is not reliable in all implementations in use.
应该返回 2017-04-27T00:00:00Z(即它应该根据 ECMA-262 解析为 UTC,这与将其视为本地的 ISO 8601 相悖),但是这在所有使用的实现中并不可靠。
If you just want to get the current date in ISO 8601 format, you can do:
如果您只想以 ISO 8601 格式获取当前日期,您可以执行以下操作:
if (!Date.prototype.toISODate) {
Date.prototype.toISODate = function() {
return this.getFullYear() + '-' +
('0'+ (this.getMonth()+1)).slice(-2) + '-' +
('0'+ this.getDate()).slice(-2);
}
}
console.log(new Date().toISODate());
However, since the built-in toISOStringuses UTC this might be confusing. If the UTC date is required, then:
但是,由于内置toISOString使用 UTC,这可能会令人困惑。如果需要 UTC 日期,则:
if (!Date.prototype.toUTCDate) {
Date.prototype.toUTCDate = function() {
return this.getUTCFullYear() + '-' +
('0'+ (this.getUTCMonth()+1)).slice(-2) + '-' +
('0'+ this.getUTCDate()).slice(-2);
}
}
console.log(new Date().toUTCDate());
回答by li x
You can achieve this by simply formating everything that you need in place using moment.format()and then simply append the extra Z to the string. You have to do this as Z within moment JS is reserved for outputting.
您可以通过简单地格式化您需要的所有内容,moment.format()然后简单地将额外的 Z 附加到字符串中来实现这一点。您必须这样做,因为 Z 在 JS 保留用于输出的时刻内。
var date = moment('2014-08-28').format("YYYY-MM-DDT00:00:00.000") + "Z";
Fiddle example https://jsfiddle.net/2avxxz6q/
回答by onmyway133
You don't need moment.jslibrary, you can just parse and connect date components. For example
您不需要moment.js库,您只需解析和连接日期组件即可。例如
type Components = {
day: number,
month: number,
year: number
}
export default class DateFormatter {
// 2018-11-11T00:00:00
static ISOStringWithoutTimeZone = (date: Date): string => {
const components = DateFormatter.format(DateFormatter.components(date))
return `${components.year}-${components.month}-${components.day}T00:00:00`
}
static format = (components: Components) => {
return {
day: `${components.day}`.padStart(2, '0'),
month: `${components.month}`.padStart(2, '0'),
year: components.year
}
}
static components = (date: Date): Components => {
return {
day: date.getDate(),
month: date.getMonth() + 1,
year: date.getFullYear()
}
}
}
回答by Carlos Sanchez Correa
You can use https://momentjs.com/docs/#/displaying/as-iso-string/
您可以使用https://momentjs.com/docs/#/displaying/as-iso-string/
moment().toISOString(keepOffset);
回答by Sarath Ak
You can try this simple javascript
你可以试试这个简单的javascript
(new Date()).toJSON().replace(/\..*$/g,'')
回答by Smit
I suppose you wish to get the current date in ISO 8601 to pass it to your API? Maybe using a moment.js wrapper for native JavaScript mentioned heremight help?
我想您希望获取 ISO 8601 中的当前日期以将其传递给您的 API?也许对这里提到的原生 JavaScript 使用 moment.js 包装器可能会有所帮助?
As in your example:
如您的示例所示:
document.write(moment('01/12/2016', 'DD/MM/YYYY', true).toISOString());
Snippet:
片段:
$(function(){
var displaysTime = $('#divTime');
displaysTime.text(moment.utc('27/04/2017', 'DD/MM/YYYY', true).toISOString());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="divTime">
</div>
回答by imjosh
I believe you are saying you'd like to get the current date in Lisbon:
我相信您是说您想在里斯本获取当前日期:
moment().tz("Europe/Lisbon").format('YYYY-MM-DD')
You can see that this works the way you think it should:
您可以看到这按照您认为应该的方式工作:
moment('2017-04-28T00:00:00+01:00').tz("Europe/Lisbon").format('YYYY-MM-DD') //"2017-04-28"

