Basic Date()
I won’t talk about it~ : )
I don’t know if anyone has encountered this problem? I think if you have written a date component, you must have this problem. My solution at the time was as follows:
For the following three methods, I used 0 as the month parameter based on the month definition of Date in JS itself. January
const EVERY_MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; function getDays(year, month) { if (month === 1 && isLeap(year)) return 29; return EVERY_MONTH_DAYS[month]; }
is to manually map the number of days in each month. If it is February and a leap year, the number of days +1
Any calendar plug-in for osx written by Amway http://www.php.cn/
Isn’t there a better way? It would be nice if there was no logic for manual mapping and leap year judgment.
function getDays(year, month) { if (month === 1) return new Date(year, month, 29).getMonth() === 1 ? 29 : 28; return new Date(year, month, 31).getMonth() === month ? 31 : 30; }
We found that the third parameter of new Date()
can Greater than the last day of each month that we know, such as:
new Date(2016, 0, 200) //Mon Jul 18 2016 00:00:00 GMT+0800 (CST)
In this way, we will use the characteristics of this JS to use 29 and 31 Two key points to determine whether it is still that month except the last day of that month + 1? (Actually, 28 and 30 are the key points).
function getDays(year, month) { return new Date(year, month + 1, 0).getDate(); }
## The third parameter of new Date() is less than 1 What will happen to the value? For example, if we pass 0, we will get the last day of the last month. Of course, there is no problem if we pass a negative number:
new Date(2016, 0, -200) //Sun Jun 14 2015 00:00:00 GMT+0800 (CST)
toGMTString() is:
Returns a string representing the Date based on the GMT ( UT) time zone. Use toUTCString() instead.UTC(Universal Standard Time)Coordinated Universal Time, also known as Universal Standard Time or Universal Coordinated Time, referred to as UTC (from The English "Coordinated Universal Time" / the French "Temps Universel Cordonné") is the most important world time standard. It is based on the atomic time and second, and is as close as possible to Greenwich Mean TimeCST( Beijing time)Beijing time, China Standard Time, China standard time. In terms of time zone division, it belongs to the East Eighth District, which is 8 hours ahead of Coordinated Universal Time and is recorded as UTC+8. However, the abbreviation of CST is more confusing because it can represent four different times at the same time:
if ( !Date.prototype.toISOString ) { ( function() { function pad(number) { if ( number < 10 ) { return '0' + number; } return number; } Date.prototype.toISOString = function() { return this.getUTCFullYear() + '-' + pad( this.getUTCMonth() + 1 ) + '-' + pad( this.getUTCDate() ) + 'T' + pad( this.getUTCHours() ) + ':' + pad( this.getUTCMinutes() ) + ':' + pad( this.getUTCSeconds() ) + '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z'; }; }() ); }
.valueOf() functions and
.getTime() Same.
valueOf, the instance of
Date cannot be operated.
var obj = Object.create(null); obj + 1; // Uncaught TypeError: Cannot convert object to primitive value(…)
new Date().toJSON() // "2016-05-05T06:03:28.130Z"
JSON.stringify(new Date()) // ""2016-05-05T06:06:02.615Z""
JSON.parse(JSON.stringify(new Date())) // "2016-05-05T06:19:24.766Z" JSON.parse('"' + new Date().toJSON() + '"') // "2016-05-05T06:19:24.766Z"
new Date().
.toLocaleDateString()
.toLcale various Strings (locales [, options]])
妈的这个 API 有点烦,看 MDN 的文档你就知道。这个 API 是用来本地化时间的。
这里稍微说下我对这些参数的理解:
locales
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // formats below assume the local time zone of the locale; // America/Los_Angeles for the US // US English uses month-day-year order alert(date.toLocaleString("en-US")); // → "12/19/2012, 7:00:00 PM" // British English uses day-month-year order alert(date.toLocaleString("en-GB")); // → "20/12/2012 03:00:00" // Korean uses year-month-day order alert(date.toLocaleString("ko-KR")); // → "2012. 12. 20. 오후 12:00:00" // Arabic in most Arabic speaking countries uses real Arabic digits alert(date.toLocaleString("ar-EG")); // → "٢٠/١٢/٢٠١٢ ٥:٠٠:٠٠ ص" // for Japanese, applications may want to use the Japanese calendar, // where 2012 was the year 24 of the Heisei era alert(date.toLocaleString("ja-JP-u-ca-japanese")); // → "24/12/20 12:00:00" // when requesting a language that may not be supported, such as // Balinese, include a fallback language, in this case Indonesian alert(date.toLocaleString(["ban", "id"])); // → "20/12/2012 11.00.00"
以locales
所指的地区的时区和语言输出。
options
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
localeMatcher
选择本地匹配的什么算法,似乎没什么大用
timeZone
再设置下 UTC 时区
hour12
是否12小时制
formatMatcher
各日期时间单元的格式化
weekday
Possible values are "narrow", "short", "long"
.
era
Possible values are "narrow", "short", "long"
.
year
Possible values are "numeric", "2-digit"
.
month
Possible values are "numeric", "2-digit", "narrow", "short", "long"
.
day
Possible values are "numeric", "2-digit"
.
hour
Possible values are "numeric", "2-digit"
.
minute
Possible values are "numeric", "2-digit"
.
second
Possible values are "numeric", "2-digit"
.
timeZoneName
Possible values are "short", "long"
.
栗子:
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); date.toLocaleString("en-US", {hour12: false}); // "12/19/2012, 19:00:00" var options = {timeZoneName:'long',weekday: "long", year: "2-digit", month: "narrow", day: "numeric"}; date.toLocaleString("en-US", options); // "Thursday, D 20, 12, China Standard Time"
插一个JavaScript 显示 Y-m-d H:i:s 的日期时间格式
let date = new Date(); let result = [ [ date.getFullYear(), date.getMonth() + 1, date.getDate() ].join('-'), [ date.getHours(), date.getMinutes(), date.getSeconds() ].join(':') ].join(' ').replace(/\b\d\b/g, '0$&');
var date = new Date(); var result = date.toLocaleString('zh-CN', { hour12: false }) .replace(/\//g, '-').replace(/\b\d\b/g, '0$&');
https://github.com/moment/moment
https://github.com/rmm5t/jquery-timeago
以上就是关于JavaScript 的 Date 最详细解读的内容,更多相关内容请关注PHP中文网(www.php.cn)!