最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Date object returns 'invalid date' for my date string - Stack Overflow

programmeradmin11浏览0评论

I want to create a Date object in Javascript using this string 04/21/2014 12:00p When passed to the constructor (new Date('04/21/2014 12:00p')), it returns Invalid Date.

I've seen other posts which manipulate the string in order to fulfill the requirements of a valid dateString, however that is not what I want. I want Javascript to recognize my date format (m/dd/yy h:mmt). In Java, something like that is simple, I imagine that there would be a similar way in Javascript.

How can I get the Date object to recognize my format?

I want to create a Date object in Javascript using this string 04/21/2014 12:00p When passed to the constructor (new Date('04/21/2014 12:00p')), it returns Invalid Date.

I've seen other posts which manipulate the string in order to fulfill the requirements of a valid dateString, however that is not what I want. I want Javascript to recognize my date format (m/dd/yy h:mmt). In Java, something like that is simple, I imagine that there would be a similar way in Javascript.

How can I get the Date object to recognize my format?

Share Improve this question asked Apr 24, 2014 at 20:51 tomatotomato 8416 gold badges17 silver badges33 bronze badges 7
  • If you don't want to split your date into pieces and work with them manually, you must use a library which does it for you. But AFAIK there's no way to do it natively. – Oriol Commented Apr 24, 2014 at 20:54
  • 2 You could extend the native object to accept the Java format and build a valid date object, that way you only need to do it in code one time.. – Brett Weber Commented Apr 24, 2014 at 20:56
  • another mon method is to pass the milliseconds of the date and use that in the constructor instead of a string – Brett Weber Commented Apr 24, 2014 at 20:57
  • @Brett do you mean convert the date string to an epoch value? How would I get the that from a string? – tomato Commented Apr 24, 2014 at 21:05
  • Do you have access to the date object in you Java code? if so, switching your mechanism of passing that date as a string to passing it as a double representing the milliseconds since January 1, 1970 (epoch value). This elliminates the headaches other than normalizing it to local time, which is another issue in itself – Brett Weber Commented Apr 24, 2014 at 21:08
 |  Show 2 more ments

3 Answers 3

Reset to default 4

This is trivial only when using a library like moment.js:

var dt = moment("04/21/2014 12:00p","MM/DD/YYYY h:mma").toDate();

Otherwise, you would have considerable string manipulation to do. Also you would have to account for users in parts of the world that use m/d/y or other formatting instead of the y/m/d formatting of your input string.

If this string is being sent from some back-end process, you might consider changing the format to a standard interchange format like ISO-8601 instead. Ex. "2014-04-21T12:00:00"

To manipulate the string in order to fulfill the requirements, could be a way, but you need to take care of all browser issues.

A more quick and dirty way is use moment.js library. It helps on formatting matters too.

if (String.prototype.dateFromJava == null)
{
   String.prototype.fromJava = function (sDateString)
   {
      var aDateOrTime = sDateString.splt(" ");
      var aDateParts  = aDateOrTime[0].split("/");
      var aTimeParts  = aDateOrTime[1].split(":");
      var oDate       = null;  

      /* just get the pieces and passing them in to new Date(), return oDate */ 
   }
}
发布评论

评论列表(0)

  1. 暂无评论