同时将日期转换为显示NaN的日期对象,以代替以角度表示的日期

while converting date into date object showing NaN in place of date in angular?

本文关键字:日期 表示 对象 转换 显示 NaN      更新时间:2023-09-26

我有一个来自服务器的日期格式,显示的日期和时间如下:"2016年1月7日星期四11:00:40 GMT+0530(IST)",我已将该日期转换为"2016年7月1日上午11:00"。我面临的问题是,我想要一个mm/dd/yyyy格式的日期,以及上午和下午12小时格式的时间,它只在chrome浏览器中显示这种格式的日期。但当我尝试在i pad中创建项目的构建时,它显示的是"楠"而不是日期和时间。

请帮忙。

这是我的日期代码:

此函数将我的日期转换为dd/mm/yyyy格式和12小时格式的时间。

  function formatDate(date) {
                var hours = date.getHours();
                var minutes = date.getMinutes();
                var ampm = hours >= 12 ? 'pm' : 'am';
                hours = hours % 12;
                hours = hours ? hours : 12; // the hour '0' should be '12'
                minutes = minutes < 10 ? '0'+minutes : minutes;
                var strTime = hours + ':' + minutes + ' ' + ampm;
                return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
                }

这段代码从json中提取日期,并将其转换为指定的格式。

                           var date = new Date(data.Steps.records[i].CreatedDate);
                            //console.log("my date" + typeof(data.Steps.records[i].CreatedDate));
                            var convertedDate = formatDate(date);
                            console.log("my new date " + convertedDate);
                            data.Steps.records[i].CreatedDate = convertedDate;
                            // console.log("format" + data.Steps.records[i].CreatedDate);
                            console.log("date is :" + date);

如果data.Steps.records[i].CreatedDate的值类似于"Thu Jan 07 2016 11:00:40 GMT+0530(IST)",则您正在尝试使用Date构造函数解析字符串。这不是一个好主意,因为对此类字符串的解析在很大程度上取决于实现,所以即使它在一个主机上工作,在另一个主机中也可能不工作。

您应该使用自己的函数或库手动解析字符串。

一旦正确创建了日期,就可以创建一个格式化的字符串,注意它将基于主机的系统设置(即"lolcal"日期)。

下面是一个解析OP中格式的函数,尽管如果你有很多不同格式的字符串,一个库可能会让生活更轻松(尽管更通用的解析器并不难写):

var s = "Thu Jan 07 2016 11:00:40 GMT+0530 (IST)"
var t = "Thu Jan 07 2016 01:10:50 GMT+0530 (IST)"
var u = "Thu Jan 07 2016 01:40:10 GMT+0530 (IST)"
function parseDate(s) {
  // Map month names to numbers suitable for input to Date
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  // Get values from the string, split on space and colon
  var b = s.split(/[ :]/);
  // Get the offset value
  var offset = b[7].replace(/'D/g, '');
  // Get the offset sign
  var offSign = b[7].indexOf('+') == -1? 1 : -1;
  
  // Create a new Date and adjust the values by the offset
  return new Date(
    Date.UTC(b[3],                          // Use year directly
    months[b[1].toLowerCase().substr(0,3)], // Get month number from map
    b[2],                                   // Use day number directly
    +b[4] + (offset.substr(0,2)*offSign),   // Adjust hours for offset
    +b[5] + (offset.substr(2,2)*offSign),   // Adjust minutes for offset
    b[6]));                                 // Use seconds directly
}
// Format date as dd/mm/yyy hh:mm:ss a/p
function formatDate(d) {
  function z(n){return ('0'+n).slice(-2)}
  var hr = d.getHours();
  var ap = hr < 12? 'am' : 'pm';
  hr = hr%12 || 12;
  return z(d.getDate()) + '/' + z(d.getMonth()+1) + '/' + d.getFullYear() +
         ' ' + z(hr) + ':' + z(d.getMinutes()) + ':' + z(d.getSeconds()) + ' ' + ap;
}
document.write(formatDate(parseDate(s)) + '<br>' +
               formatDate(parseDate(t)) + '<br>' +
               formatDate(parseDate(u)));

编辑

添加函数以格式化每个OP的日期字符串。