Javascript 如何将ISO日期字符串更改为日期对象?
我被困在一个奇怪的情况下,不幸的是,即使做了一些RnD和谷歌搜索,我也无法解决这个问题 我有一个ISO格式的日期字符串,如Javascript 如何将ISO日期字符串更改为日期对象?,javascript,date,datetime,Javascript,Date,Datetime,我被困在一个奇怪的情况下,不幸的是,即使做了一些RnD和谷歌搜索,我也无法解决这个问题 我有一个ISO格式的日期字符串,如2014-11-03T19:38:34.203Z,我想用new date()方法将其转换为日期对象 但当我这样做时,输出是: var isoDate = '2014-11-03T19:38:34.203Z'; console.log(new Date(isoDate)); //output is: Tue Nov 04 2014 01:08:34 GMT+0530 (IST)
2014-11-03T19:38:34.203Z
,我想用new date()
方法将其转换为日期对象
但当我这样做时,输出是:
var isoDate = '2014-11-03T19:38:34.203Z';
console.log(new Date(isoDate)); //output is: Tue Nov 04 2014 01:08:34 GMT+0530 (IST)
我传递的日期是2014年11月3日,输出是2014年11月4日,这是因为我们当地时间(IST)的GMT+5.30
那么,是否有任何通用方法可以用来获取返回2014年11月3日日期的date
对象
注意:我对时间戳没有任何问题。我们可以使用
setHours()
方法将时间字符串更改为零。我唯一想要的是date
对象,比如new date()
日期为2014年11月3日这取决于你以后想对对象做什么。您可以随时参考javascript的“UTC”日期函数
检查参考:
不要将字符串传递给日期构造函数,因为它在解析字符串方面非常糟糕。例如,IE8根本不会解析ISO8601格式的字符串并返回NaN。编写自己的解析器非常简单:
function parseISOString(s) {
var b = s.split(/\D+/);
return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6]));
}
另请注意,如果时间为19:38:34.203 UTC,且您的时区为UTC+0530,则该时区的时间为次日凌晨01:08:34,因此存在日期差异。例如,对于在澳大利亚东海岸但不遵守夏令时(即UTC+10)的人,这相当于:
4 November, 2014 05:38:34
编辑
因此,如果您想将其返回到ISO日期,您可以使用这些方法创建任何适合的格式,例如
function isoFormatDMY(d) {
function pad(n) {return (n<10? '0' : '') + n}
return pad(d.getUTCDate()) + '/' + pad(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear();
}
var s = '2014-11-03T19:38:34.203Z';
var date = parseISOString(s);
console.log(isoFormatDMY(date)) // 03/11/2014
适用于ES5之前版本浏览器的简单多边形填充:
if (!Date.prototype.toISOString) {
Date.prototype.toISOString = function() {
var d = this;
// Padding functions
function pad(n) {return (n<10? '0' : '') + n}
function padd(n){return (n<100? '0' : '') + pad(n)}
return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) +
'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' +
pad(d.getUTCSeconds()) + '.' + padd(d.getMilliseconds()) + 'Z';
}
}
if(!Date.prototype.toISOString){
Date.prototype.toISOString=函数(){
var d=这个;
//填充函数
函数pad(n){return(n您可以使用“getUTCDate()”获取实际日期
var d = new Date('2014-11-03T19:38:34.203Z');
var n = d.getUTCDate();
但它将只返回日期。若要获取月份“getUTCMonth()”和年份“getUTCFullYear()”,请将所有内容都构造为您的格式。例如
var n=[];
var d = new Date('2014-11-03T19:38:34.203Z');
var s = d.getUTCDate();
n.push(s);
s=d.getUTCMonth();
n.push(s);
s=d.getUTCFullYear();
n.push(s);
最后,将n
作为一个对象。出于显示目的,您可以简单地执行此操作。。。
给定您可以实施的ISO格式的“日期”
<div> {{Date | date:"dd MMM yy hh:mm a" }} </div>
{Date | Date:“dd-MMM-yy-hh:mm-a”}
以所需格式显示(仅显示)日期的巧妙技巧
希望有帮助!我也不关心时间戳/时区,因为我只从SQL返回ISO格式的日期。为了避免在转换为日期对象时日期提前或延迟,以下方法有效:
moment(ISOStringHere, 'YYYY-MM-DD HH:mm'); // leaving off Z makes it UTC to match database
这需要位于此处的JS库:
对于那些需要支持旧浏览器并正确调整日期字符串中时区的用户来说,这是一个很好的功能。我使用它作为起始点,但不得不对它进行大量修改,因为它不适用于带有诸如“-07:00”等时区偏移量的字符串(即不以“Z”结尾的字符串)
用法/一对测试:
// All three of these tests output the same date (relative to your
// timezone) as they should, which in my case is:
// "Thu Jan 17 2019 17:00:00 GMT-0700 (Mountain Standard Time)".
console.log( isoStringToDate( "2019-01-18T00:00:00.000Z" ) );
console.log( isoStringToDate( "2019-01-17T17:00:00.000-07:00" ) );
console.log( isoStringToDate( "2019-01-18T07:00:00.000+07:00" ) );
一开始我的答案非常好,但因为我需要这个代码生成器,所以我关心代码大小。最终我得到了以下结果:
const parseDate=dateString=>{
常量b=dateString.split(/\D+/);
const offsetMult=dateString.indexOf('+')!==-1?-1:1;
常数hrOffset=offsetMult*(+b[7]| | 0);
常数minOffset=offsetMult*(+b[8]| | 0);
返回新日期(Date.UTC(+b[0]、+b[1]-1、+b[2]、+b[3]+hrOffset、+b[4]+minOffset、+b[5]、+b[6]|0));
};
角度datepipe转换将其转换为不带时区的简单日期时间格式。对于ISO字符串格式,日期构造函数将其视为UTC时间,但现在,日期构造函数将其视为本地时间
我的例子(IST):
我想要一个date
对象,我在剑道图表中进一步使用它来表示数据。使用date.UTC
,我想,我无法获取date
对象。您不必更改对象本身。您只需使用该方法获取ex.UTC字符串-.toutstring()你是对的。但根据我的要求,我需要显示ISO日期(在我的情况下,是2014年11月3日)。我们还需要在2019年这样做吗?特别是nodejs。我应该可以使用“新日期”@Pathfinder是,不是。ECMA-262支持两种格式,在受控环境中应该可以,但在其他地方避免对所有字符串使用内置解析器。@RobG我使用的是堆栈上的类似脚本:function parseDate(s){var b=s.split(/\D/);返回新日期(b[0],b[1]-1,b[2],b[3],b[4],b[5]);]
我正试图将时间向前移动一个小时以进行时区更改,唯一有效的方法是返回新日期(b[0],b[1]-1,b[2],b[3],b[4],36+b[5]);}
因为似乎每个单位在b[5]之前都会增加
等于100秒,所以3600/100=36。有没有更系统的方法来做到这一点,或者只是一些参考来更好地理解,而不是通过尝试和错误?如果你想向前移动1小时,那么数字(b[3])+1
应该可以。您正在处理字符串,因此应该将其转换为数字,以便+
成为加法而不是串联。36+b[5]
将字符串文字“36”前置为b[5]的任何值。如果它是“00”,您将得到“3600”我猜这是一个小时的广告,但这对我来说有点麻烦。你看到问题的描述和问题的标签了吗,都包括纯javascript代码。没有角度用法或任何JS框架包含绑定数据,所以你的答案不符合主题
// Parse an ISO date string (i.e. "2019-01-18T00:00:00.000Z",
// "2019-01-17T17:00:00.000-07:00", or "2019-01-18T07:00:00.000+07:00",
// which are the same time) and return a JavaScript Date object with the
// value represented by the string.
function isoStringToDate( isoString ) {
// Split the string into an array based on the digit groups.
var dateParts = isoString.split( /\D+/ );
// Set up a date object with the current time.
var returnDate = new Date();
// Manually parse the parts of the string and set each part for the
// date. Note: Using the UTC versions of these functions is necessary
// because we're manually adjusting for time zones stored in the
// string.
returnDate.setUTCFullYear( parseInt( dateParts[ 0 ] ) );
// The month numbers are one "off" from what normal humans would expect
// because January == 0.
returnDate.setUTCMonth( parseInt( dateParts[ 1 ] - 1 ) );
returnDate.setUTCDate( parseInt( dateParts[ 2 ] ) );
// Set the time parts of the date object.
returnDate.setUTCHours( parseInt( dateParts[ 3 ] ) );
returnDate.setUTCMinutes( parseInt( dateParts[ 4 ] ) );
returnDate.setUTCSeconds( parseInt( dateParts[ 5 ] ) );
returnDate.setUTCMilliseconds( parseInt( dateParts[ 6 ] ) );
// Track the number of hours we need to adjust the date by based
// on the timezone.
var timezoneOffsetHours = 0;
// If there's a value for either the hours or minutes offset.
if ( dateParts[ 7 ] || dateParts[ 8 ] ) {
// Track the number of minutes we need to adjust the date by
// based on the timezone.
var timezoneOffsetMinutes = 0;
// If there's a value for the minutes offset.
if ( dateParts[ 8 ] ) {
// Convert the minutes value into an hours value.
timezoneOffsetMinutes = parseInt( dateParts[ 8 ] ) / 60;
}
// Add the hours and minutes values to get the total offset in
// hours.
timezoneOffsetHours = parseInt( dateParts[ 7 ] ) + timezoneOffsetMinutes;
// If the sign for the timezone is a plus to indicate the
// timezone is ahead of UTC time.
if ( isoString.substr( -6, 1 ) == "+" ) {
// Make the offset negative since the hours will need to be
// subtracted from the date.
timezoneOffsetHours *= -1;
}
}
// Get the current hours for the date and add the offset to get the
// correct time adjusted for timezone.
returnDate.setHours( returnDate.getHours() + timezoneOffsetHours );
// Return the Date object calculated from the string.
return returnDate;
}
// All three of these tests output the same date (relative to your
// timezone) as they should, which in my case is:
// "Thu Jan 17 2019 17:00:00 GMT-0700 (Mountain Standard Time)".
console.log( isoStringToDate( "2019-01-18T00:00:00.000Z" ) );
console.log( isoStringToDate( "2019-01-17T17:00:00.000-07:00" ) );
console.log( isoStringToDate( "2019-01-18T07:00:00.000+07:00" ) );
new Date(this.datePipe.transform(isoDate,'yyyy-MM-dd HH:mm:ss', 'UTC'));
input string: 2020-04-19T09:15:00.000Z
after transform this.datePipe.transform(input string)
2020-04-19 09:15:00
Date object
new Date(this.datePipe.transform(isoDate,'yyyy-MM-dd HH:mm:ss', 'UTC'));//treats the transformed date as local timezone date when creating the date object
Sun Apr 19 2020 09:15:00 GMT+0530 (India Standard Time)