Angularjs 新日期总是迟1天返回
我为new Date()编写了以下代码,以及一个将new Date()格式化为更易于理解的函数:Angularjs 新日期总是迟1天返回,angularjs,date,servicenow,Angularjs,Date,Servicenow,我为new Date()编写了以下代码,以及一个将new Date()格式化为更易于理解的函数: data.onbCase.push({ first_name: onbCase.getDisplayValue('subject_person.first_name'), last_name: onbCase.getDisplayValue('subject_person.last_name'),
data.onbCase.push({
first_name: onbCase.getDisplayValue('subject_person.first_name'),
last_name: onbCase.getDisplayValue('subject_person.last_name'),
start_date: formatDate(new Date(onbCase.getDisplayValue('hr_profile.employment_start_date')))
});
function formatDate(date) {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var day = date.getDate(),
monthIndex = date.getMonth(),
year = date.getFullYear();
return new Date(monthNames[monthIndex].substr(0,3) + ' ' + day + ', ' + year);
}
<td ng-show="c.options.start_date" title="{{item.start_date}}">{{item.start_date | date:'mediumDate'}}</td>
但这似乎不正确……有什么建议吗?有关更多信息,请参阅此问题:
new Date()
字符串解析器不可靠,因为不同的字符串日期格式会得到不同的结果
new Date()
// Fri Feb 15 2019 15:08:14 GMT-0600 (Central Standard Time)
new Date("2019-02-15")
// Thu Feb 14 2019 18:00:00 GMT-0600 (Central Standard Time)
new Date("2019-02-15 00:00:00")
// Thu Feb 15 2019 18:00:00 GMT-0600 (Central Standard Time)
new Date("02/15/2019")
// Fri Feb 15 2019 00:00:00 GMT-0600 (Central Standard Time)
某些日期格式,特别是如果您不包含时间信息,会将您的日期视为源自格林威治标准时间,然后根据计算机的区域设置对日期进行偏移。这将导致日期被负6小时(CST)抵消,有效地将您的一天更改为前一天
new Date()
// Fri Feb 15 2019 15:08:14 GMT-0600 (Central Standard Time)
new Date("2019-02-15")
// Thu Feb 14 2019 18:00:00 GMT-0600 (Central Standard Time)
new Date("2019-02-15 00:00:00")
// Thu Feb 15 2019 18:00:00 GMT-0600 (Central Standard Time)
new Date("02/15/2019")
// Fri Feb 15 2019 00:00:00 GMT-0600 (Central Standard Time)