js string to date, js string parsed into date, js date parsing,
Date.parse hour is 8 o'clock, Date.parse time is 8 hours more
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright Sweet Potato Yao February 16, 2017
http://fanshuyao.iteye.com/
1. In js, the official website of string to date is like this:
Date.parse(datestring)
Where datestring is a date string.
For example, see: http://www.w3school.com.cn/tiy/t.asp?f=jseg_parse
The parse() method parses a datetime string and returns the number of milliseconds since midnight on 1/1/1970 from the datetime.
Return value: The number of milliseconds between the specified date and time and midnight (GMT time) on 1/1/1970.
Second, the datestring string format problem.
When datestring=2017-02-16, the parsed result is:
Thu Feb 16 2017 08:00:00 GMT+0800 (China Standard Time)
code show as below:
<html> <body> <script type="text/javascript"> var d = new Date(Date.parse("2017-02-16")); document.write(d) </script> </body> </html>
It's 08:00:00 here , not 00:00:00, which should relate to the timezone.
When datestring=2017/02/16, the result of parsing is:
Thu Feb 16 2017 00:00:00 GMT+0800 (China Standard Time)
code show as below:
<html> <body> <script type="text/javascript"> var d = new Date(Date.parse("2017/02/16")); document.write(d) </script> </body> </html>
As can be seen from the above code, js string to date should pay attention to the format of the date string.
Three, string to date method:
/** * Date parsing, string to date * @param dateString can be 2017-02-16, 2017/02/16, 2017.02.16 * @returns {Date} returns the corresponding date object */ function dateParse(dateString){ var SEPARATOR_BAR = "-"; var SEPARATOR_SLASH = "/"; var SEPARATOR_DOT = "."; var dateArray; if(dateString.indexOf(SEPARATOR_BAR) > -1){ dateArray = dateString.split(SEPARATOR_BAR); }else if(dateString.indexOf(SEPARATOR_SLASH) > -1){ dateArray = dateString.split(SEPARATOR_SLASH); }else{ dateArray = dateString.split(SEPARATOR_DOT); } return new Date(dateArray[0], dateArray[1]-1, dateArray[2]); };
日期格式化成字符串:
http://fanshuyao.iteye.com/blog/1687820
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright 蕃薯耀 2017年2月16日
http://fanshuyao.iteye.com/