JS string date Yyyy-mm-dd converted to date sample code

Source: Internet
Author: User
Tags json

  Get the date in the form back to the table through the JSON pass, encountered Date.parse (str) function in FF under the error, there are similar cases of friends can refer to this article

Recently encountered a problem, is to get the date in the form back to pass through the JSON way, encountered Date.parse (str) function in FF under the error: nan    Find some information, found that the Date.parse () function on the date format requirements: Detailed reference Date.parse function     for JS operation date:    Create a Date object:    var objdate=new date ([arguments list]); nbsp The   Parameter form has the following 5:    codes as follows: View plainnew date ("Month dd,yyyy hh:mm:ss");  new Date ("Month dd,yyyy");  New Date (YYYY,MTH,DD,HH,MM,SS);  new Date (YYYY,MTH,DD);  new Date (ms);    Description:    Month: Month names in English, from January to december  MTH: The month is expressed in integers, from 0 (January) to 11 (December)   content    DD: Represents the day ordinal of one months, from 1 to 31  YYYY: Year of four digits   hh: Hours, from 0 (midnight) to 23 (11 o'clock in the evening)   mm: minutes, integers from 0 to 59   SS: seconds, integers from 0 to 59   MS: milliseconds, for integers greater than or equal to 0, Represents the number of milliseconds to create and the difference between GMT time January 1, 1970.     I found the construction of dates in:    JavaScript can also support new date ("Yyyy/mm/dd"); Where: MM is an integer representing the month from 0 (January) to 11 (December), which makes it easy to convert the string date by using regular expressions.     Test Code:    code as follows: <mce:script type= "Text/javascript" ><!--  document.write ("<br/ > "+ new Date (" FEbruary 3,2009 "));  document.write (" <br/> "+ New Date (" February 3,2009 10:52:03 "));  document.write (" <br/> ");  document.write (" <br/> "+ new Date (2009,1,3));  document.write (" <br/> "+ new Date (2009,1,3,10,52,03));   document.write ("<br/>");  document.write ("<br/>" + New Date (Date.parse ("February 3,2009"));   document.write ("<br/>" + New Date (Date.parse ("February 3,2009 10:52:03"));  document.write ("<br /> "+ New Date (Date.parse (2009,1,3))); output:nan  document.write ("<br/>" + New Date (Date.parse (2009,1,3,10,52,03)); output:nan  document.write ("<br/>" + New Date (Date.parse ("2009/02/03"));  document.write ("<br /> ");  document.write (" <br/> "+ new Date (" 2009/02/03 "));  document.write (" <br/> "+ New Date (" 2009/02/03 11:12:13 "));  document.write (" <br/> "+ New Date (" 2009-02-03 ")); output:nan   //--></mce:script>&nbsp   Output:    Tue Feb 3 00:00:00 utc+0800 2009  Tue Feb 3 10:52:03 utc+0800 2009    Tue Feb 3 0 0:00:00 utc+0800 2009  Tue Feb 3 10:52:03 utc+0800 2009    Tue Feb 3 00:00:00 utc+0800 2009  Tue Feb 3 10:52:03 utc+0800 2009  nan  nan  Tue Feb 3 00:00:00 utc+0800 2009    Tue Feb 3 00:00:00 UTC+080 0 2009  Tue Feb 3 11:12:13 utc+0800 2009  nan -------------------    code is as follows: Window.onload=functio N () {  var dependedval= "2005-3-4"; //converted to date by date string   var regEx = new RegExp ("-", "GI");  dependedval= Dependedval.replace (RegEx, "/"); //dependedval=dependedval.replace ("-", "/");/This is not OK   alert (dependedval)  //parse needs 2005/3/4 this format   var milliseconds=date.parse (dependedval);  alert (milliseconds)   VAR Dependeddate=new date ();  dependeddate.settime (milliseconds);    var now = new Date (); //Note parentheses, Priority issues, helpless   alert ("Years apart:" + (Now.getfullyear) ()-Dependeddate.getfullyear ()); }    Actually, the date is transmitted between the browser and the server by millisecond value, otherwise it will error 400 errors!   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.