How do I get the current date in JavaScript?
如何在javascript中获取当前日期?
使用
1 2 3 4 5 6 7 | var today = new Date(); var dd = String(today.getDate()).padStart(2, '0'); var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! var yyyy = today.getFullYear(); today = mm + '/' + dd + '/' + yyyy; document.write(today); |
这将以mm/dd/yyyy格式提供今天的日期。
只需将
1 2 | var utc = new Date().toJSON().slice(0,10).replace(/-/g,'/'); document.write(utc); |
如果要重用
UPDATED!, Scroll Down
如果你想给最终用户一些简单漂亮的东西…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var objToday = new Date(), weekday = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), dayOfWeek = weekday[objToday.getDay()], domEnder = function() { var a = objToday; if (/1/.test(parseInt((a +"").charAt(0)))) return"th"; a = parseInt((a +"").charAt(1)); return 1 == a ?"st" : 2 == a ?"nd" : 3 == a ?"rd" :"th" }(), dayOfMonth = today + ( objToday.getDate() < 10) ? '0' + objToday.getDate() + domEnder : objToday.getDate() + domEnder, months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'), curMonth = months[objToday.getMonth()], curYear = objToday.getFullYear(), curHour = objToday.getHours() > 12 ? objToday.getHours() - 12 : (objToday.getHours() < 10 ?"0" + objToday.getHours() : objToday.getHours()), curMinute = objToday.getMinutes() < 10 ?"0" + objToday.getMinutes() : objToday.getMinutes(), curSeconds = objToday.getSeconds() < 10 ?"0" + objToday.getSeconds() : objToday.getSeconds(), curMeridiem = objToday.getHours() > 12 ?"PM" :"AM"; var today = curHour +":" + curMinute +"." + curSeconds + curMeridiem +"" + dayOfWeek +"" + dayOfMonth +" of" + curMonth +"," + curYear; document.getElementsByTagName('h1')[0].textContent = today; |
1 |
UBBER UPDATE After much procrastination, I've finally GitHubbed and updated this with the final solution I've been using for myself. It's even had some last minute edits to make it sweeter! If you're looking for the old jsFiddle, please see this.
此更新包含2flavors,仍然相对较小,尽管没有我上面的原始答案那么小。如果你想要非常小的,那就去吧。
另请注意:这仍然比moment.js更不膨胀。虽然moment.js很不错,但在我看来,它必须有很多世俗的方法,这些方法需要学习时间,就好像它是一种语言一样。这里的mine使用与php:date相同的通用格式。
- 日期.format.min.js 5.08 KB
- 日期格式.min.js 4.16 kb
Flavor 1
new Date().format(String)
My Personal Fav. I know the taboo, but works great on the Date Object. Just be aware of any other mods you may have to the Date Object.
1 2 | // use as simple as new Date().format('m-d-Y h:i:s'); // 07-06-2016 06:38:34 |
Flavor 2
dateFormat(Date, String)
More traditional all-in-one method. Has all the ability of the previous, but is called via method with Date param.
1 2 | // use as simple as dateFormat(new Date(), 'm-d-Y h:i:s'); // 07-06-2016 06:38:34 |
BONUS Flavor (requires jQuery)
$.date(Date, String)
This contains much more than just a simpleformat option. It extends the base Date object and includes methods such asaddDays . For more information, please see the Git.
在这个mod中,格式字符受到php:date的启发。有关完整列表,请参阅我的自述文件
这个mod还有一个更长的预先制作的格式列表。要使用预设格式,只需输入其密钥名。
- "复合"
- 'commonlogformat'=='d/m/y:g:i:s'
- 'exif'=='y:m:d g:i:s'
- 'isoYearWeek'='y\ww'
- 'isoYearWeek2'='Y-\WW'
- 'isoYearWeekDay'='Y\wwj'
- 'isoYearWeekDay2'='Y-\WW-J'
- 'mysql'='Y-M-D H:I:S'
- 'PostgreSQL'='Y.Z'
- 'postgresql2'='yz'
- 'soap'='y-m-d\th:i:s.u'
- 'soap2'='y-m-d\th:i:s.up'
- 'unixtimestamp'='@u'
- 'xmlrpc'=='YMD\tg:i:s'
- 'xmlRpcCompact'=='YMD\tgis'
- 'wddx'='y-n-j\tg:i:s'
- "常量"
- 'American'='F J Y'
- 'americanshort'='m/d/y'
- 'americanshortwtime'=='m/d/y h:i:sa'
- 'atom'='y-m-d\th:i:sp'
- 'cookie'='L D-M-Y H:I:S T'
- '欧洲'='J F Y'
- 'EuropeanShort'='d.m.y'
- 'europeanshortwtime'=='d.m.y h:i:s'
- 'ISO8601'='Y-M-D\th:i:so'
- '法律'='J F Y'
- 'rfc822'=='d d m y h:i:s o'
- 'RFC850'='L D-M-Y H:I:S T'
- 'rfc036'=='d d d m y h:i:s o'
- 'rfc1123'=='d d d m y h:i:s o'
- 'rfc2822'=='d d m y h:i:s o'
- 'rfc339'=='Y-M-D\th:i:sp'
- 'rss'='d d m y h:i:s o'
- 'W3C'='Y-M-D\th:i:sp'
- "漂亮"
- 'pretty-a'='g:i.sa l js\o\f f y'
- 'pretty-b'='g:ia l js\o\f f y'
- 'pretty-c'=='n/d/y g:ia'
- 'pretty-d'='n/d/y'
- 'pretty-e'='f js-g:ia'
- 'pretty-f'='g:ia'
正如您可能注意到的,您可以使用double
如果您只想要一个没有时间信息的日期,请使用:
1 2 3 4 | var today = new Date(); today.setHours(0, 0, 0, 0); document.write(today); |
尽可能短。
获取"2018-08-03"格式:
1 2 3 | let today = new Date().toISOString().slice(0, 10) console.log(today) |
要获得类似"8/3/2018"的格式:
1 2 3 | let today = new Date().toLocaleDateString() console.log(today) |
此外,还可以将locale作为参数传递,例如
试试这个:
1 2 3 4 5 | var currentDate = new Date() var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear() document.write("" + day +"/" + month +"/" + year +"") |
结果会像
1 | 15/2/2012 |
如果您希望对日期格式进行更细致的控制,那么我完全建议您查看momentjs。很棒的图书馆——只有5千块。网址:http://momentjs.com/
您可以使用moment.js:http://moment js.com/
1 2 3 | var m = moment().format("DD/MM/YYYY"); document.write(m); |
1 | <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"> |
1 2 3 | var d = (new Date()).toString().split(' ').splice(1,3).join(' '); document.write(d) |
要将其分解为步骤:
每次都是这样:
1 2 3 4 5 6 | var now = new Date(); var day = ("0" + now.getDate()).slice(-2); var month = ("0" + (now.getMonth() + 1)).slice(-2); var today = now.getFullYear() +"-" + (month) +"-" + (day); console.log(today); |
1 | var date = new Date().toLocaleDateString("en-US"); |
此外,还可以使用两个参数调用方法
1 2 3 4 | var date = new Date().toLocaleDateString("en-US", { "year":"numeric", "month":"numeric" }); |
关于msdn的文章。有关MDN上此方法的详细信息。
更干净、更简单的版本:
1 | new Date().toLocaleString(); |
根据用户的区域设置,结果会有所不同:
2/27/2017, 9:15:41 AM
如果你是快乐和YYYY-MM-DD格式,这将做的工作为好。 </P >
您可以使用日期.js库来扩展日期对象,因此您可以使用.today()方法。
您现在可以获取当前日期调用静态方法,如下所示:
1 | var now = Date.now() |
参考文献:
https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/date/now
瓦伦的回答不考虑TimeZoneOffset。以下是一个版本:
1 2 | var d = new Date() new Date(d.getTime() - d.getTimezoneOffset() * 60000).toJSON().slice(0, 10) // 2015-08-11 |
最短的回答是:
如果你想要一个简单的
1 2 | var d = new Date(); document.write( [d.getDate(), d.getMonth()+1, d.getFullYear()].join('/') ); |
1 | new Date().toDateString(); |
结果:
"Wed Feb 03 2016"
作为
1 | document.write(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('T')[0]); |
</P >
对山羊的日期和时间格式为进入
1 | document.write(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0]); |
</P >
对山羊的日期和时间格式为进入
1 | document.write(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0].replace('T',' ')); |
</P >
1 | new Date().toISOString().slice(0,10); |
也会工作
你可以用这个
1 2 3 4 5 6 7 8 | function my_curr_date() { var currentDate = new Date() var day = currentDate.getDate(); var month = currentDate.getMonth() + 1; var year = currentDate.getFullYear(); var my_date = month+"-"+day+"-"+year; document.getElementById("dateField").value=my_date; } |
HTML是
1 2 3 | <body onloadx='return my_curr_date();'> <input type='text' name='dateField' id='dateField' value='' /> </body> |
如果你是使用jQuery。这一尝试,得到: </P >
1 | $.datepicker.formatDate('dd/mm/yy', new Date()); |
这里是会议的Formatting日期 </P >
- D日(7月领先零)
- DD日个月(两位数)
- 澳天杨恩(领先的零号)
- 00天杨恩(三位数)
- D日的短名称
- DD长节的名称
- M个月(NO)领先的零年)
- 毫米,年(月(两位数)
- M个月的短名称
- mm长的月的名称
- Y"(两位)
- YY年(四位数)
这里是jQuery什么控件参考指南 </P >
(一个在线的起始溶液。 </P >
tl;dr
var todaysDate = new Date(Date.now()).toLocaleString().slice(0,3).match(/[0-9]/i) ? new Date(Date.now()).toLocaleString().split(' ')[0].split(',')[0] : new Date(Date.now()).toLocaleString().split(' ')[1] +"" + new Date(Date.now()).toLocaleString().split(' ')[2] +"" + new Date(Date.now()).toLocaleString().split(' ')[3];
edge, ff latest, & chrome returntodaysDate ="2/7/2017" "works"* in IE10+
2 /编辑/ 2017年7 </P >
在那发现了IE10和IE的边缘做一件不同的事情。去的身影。 与
剧情:IE10 </P >
1 | "Tuesday, February 07, 2017 2:58:25 PM" |
我可以写一个大长和ftfy功能。但你真的应该对使用moment.js这东西。我的剧本merely cleans这和你的DPS,扩展传统的美国
剧情:IE的边缘 </P >
1 | "?2?/?7?/?2017? ?2?:?59?:?27? ?PM" |
(当然,它不能是简单的祷告。边缘的日期字符串有隐形的"?"在每一个人物的可见。所以不只是将我们现在请检查如果第一个字符是一个数字,但第一个3字,因为它打开了,任何单字符输入的日期范围和整个nbsp;将最终向一个点或一个斜杠在一些点。所以要让事情简单,正是.slice(三)第一(小字符缓冲区对未来的恶作剧),然后用支票号码。它应该是noted可能让这些不可见点的潜在可能坚持在你的代码。我知道你在考虑,如果你有bigger计划比刚刚打印这串到你的视图。 </P >
最新的一∴得到: </P >
1 | var todaysDate = new Date(Date.now()).toLocaleString().slice(0,3).match(/[0-9]/i) ? new Date(Date.now()).toLocaleString().split(' ')[0].split(',')[0] : new Date(Date.now()).toLocaleString().split(' ')[1] +"" + new Date(Date.now()).toLocaleString().split(' ')[2] +"" + new Date(Date.now()).toLocaleString().split(' ')[3]; |
读到那很烂。怎么说的: </P >
1 2 | var dateString = new Date(Date.now()).toLocaleString(); var todaysDate = dateString.slice(0,3).match(/[0-9]/i) ? dateString.split(' ')[0].split(',')[0] : dateString.split(' ')[1] +"" + dateString.split(' ')[2] +"" + dateString.split(' ')[3]; |
原始的答案 </P >
中的每一个衬你: </P >
1 | new Date(Date.now()).toLocaleString().split(', ')[0]; |
和
你可以结账
1 2 3 4 | var today = new Date(); today = parseInt(today.getMonth()+1)+'/'+today.getDate()+'/'+today.getFullYear()+" Time :"+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds(); document.write(today); |
并参见日期()构造函数的文档。链接
这有什么大不了的……最干净的方法是
1 2 3 4 5 6 7 8 9 | var dateTimeToday = new Date(); var dateToday = new Date( dateTimeToday.getFullYear(), (dateTimeToday.getMonth() + 1) /*Jan = 0! */, dateTimeToday.getDate(), 0, 0, 0, 0); |
我认为这是一个古老的问题,但最简单的方法是:
1 2 3 4 5 6 | var date = new Date(); var TimeStamp = date.toLocaleString(); function CurrentTime(){ alert(TimeStamp); } |
这将获取当前时间,并根据位置将其传递给字符串,然后您可以调用函数currentTime来显示时间。对我来说,这将是获得某物时间戳的最有效方法。
我不知道它是否能帮助任何人,但我用它来获取今日约会对象。
1 | new Date( 3600000*Math.floor(Date.now()/3600000) ) |
这对你有帮助
1 2 | var date = new Date(); console.log(date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getFullYear()); |
这将以dd/mm/yyyy格式打印当前日期
最小2.39kb。一个文件。网址:https://github.com/rhroyston/clock-js只是想帮忙…
如果你再找一批更多的约会过granular控制的形成,在thoroughly recommend检查了约会的联合国。terrific图书馆多小比moment.js和它的功能为基础的方法让它那么多的阿姨级图书馆的其他组件。提供必要的大数的行动结束的日期。 </P >
http:/ / /文档/ date-fns.org开始采掘 </P >
把日期印成这样。
June 1st, 2015 11:36:48 AM
https://gist.github.com/gerst20051/7d72693f722bb0f6b58
如果你看的到的字符串格式。 </P >
1 | statusUpdate ="time" + new Date(Date.now()).toLocaleTimeString(); |
输出时间11时30分53 AM </P >
在思想的东西,会贴在使用一个真正的约会。 </P >
1 2 3 4 5 | function realDate(date){ return date.getDate() +"/" + (date.getMonth()+1) +"/" + date.getUTCFullYear(); } var ourdate = realDate(new Date); |
这可以帮你的 </P >
1 2 3 4 | let d = new Date(); this.dateField = element(by.xpath('xpath here')); this.datetField.sendKeys((d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear()); |
1 | (function() { var d = new Date(); return new Date(d - d % 86400000); })() |
这是我目前最喜欢的,因为它既灵活又模块化。它是(至少)三个简单函数的集合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Returns an array with date / time information * Starts with year at index 0 up to index 6 for milliseconds * * @param {Date} date date object. If falsy, will take current time. * @returns {[]} */ getDateArray = function(date) { date = date || new Date(); return [ date.getFullYear(), exports.pad(date.getMonth()+1, 2), exports.pad(date.getDate(), 2), exports.pad(date.getHours(), 2), exports.pad(date.getMinutes(), 2), exports.pad(date.getSeconds(), 2), exports.pad(date.getMilliseconds(), 2) ]; }; |
PAD功能如下:
1 2 3 4 5 6 7 8 9 10 | /** * Pad a number with n digits * * @param {number} number number to pad * @param {number} digits number of total digits * @returns {string} */ exports.pad = function pad(number, digits) { return new Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number; }; |
最后,我可以手工构建日期字符串,或者使用简单的函数为我创建日期字符串:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Returns nicely formatted date-time * @example 2015-02-10 16:01:12 * * @param {object} date * @returns {string} */ exports.niceDate = function(date) { var d = exports.getDateArray(date); return d[0] + '-' + d[1] + '-' + d[2] + ' ' + d[3] + ':' + d[4] + ':' + d[5]; }; /** * Returns a formatted date-time, optimized for machines * @example 2015-02-10_16-00-08 * * @param {object} date * @returns {string} */ exports.roboDate = function(date) { var d = exports.getDateArray(date); return d[0] + '-' + d[1] + '-' + d[2] + '_' + d[3] + '-' + d[4] + '-' + d[5]; }; |
试试这个…HTML
1 2 | <p id="date"> </p> |
JS
1 2 3 4 5 | var currentDate = new Date() var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear() document.getElementById("date").innerHTML =("" + day +"/" + month +"/" + year +"") |
当前日期的工作演示
演示
你可以使用我的约会的API方法给出了下面的每天使用日期Formatting存在与认识当前日期,昨天,等。 对使用技术 E.g. </P >
1 2 3 4 5 6 7 8 | var dt = new Date(); /// ANY DATE YOU WANT -- dt = new Date(""July 21, 1983 01:15:00"") dateObj = dt.getFormattedDate(); alert( dateObj.isToday() ); alert( dateObj.todayDay() ); alert( dateObj.monthNameDayYear() ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | (function () { fnDateProcessor = function () { var that = this; return { yyyymmdd: function (separator) { var fdate = this.formatDate(true, true) , separator = separator ? separator :"-"; return fdate.year + separator + fdate.month + separator + fdate.day; }, monthNameDayYear: function () { var fdate = this.formatDate(true, true); return fdate.monthName +"" + fdate.day +"," + fdate.year; }, ddmmyyyy: function (separator) { var fdate = this.formatDate(true, true) , separator = separator ? separator :"/"; return fdate.day + separator + fdate.month + separator + fdate.year; }, meridianTime: function () { var fdate = this.formatDate(); return fdate.hour +":" + fdate.minute +"" + fdate.meridian; }, monthDay: function (separator) { var fdate = this.formatDate(); separator = checkSeparator(separator); return fdate.monthName.substring(0, 3) + separator + fdate.day; }, weekMonthDayYear: function () { var fdate = this.formatDate(); //separator = checkSeparator(separator); return fdate.weekDay +"" + fdate.monthName.substring(0, 3) + fdate.day +" ," + fdate.year; }, timeZoneInclusive: function () { return new Date(that); }, todayDay: function () { return new Date().getDate(); }, todayMonth: function () { return new Date().getMonth() + 1; }, dateDay: function () { return this.formatDate().day; }, dateMonth: function () { return this.formatDate().month; }, isToday: function () { return this.sameDate(new Date()); }, isYesterday: function () { d = new Date(); d.setDate(d.getDate() - 1); return this.sameDate(d); }, formatDate: function () { var zeroPaddedMnth = true, zeroPaddedDay = false, zeroPaddedHr = false, zeroPaddedMin = true; // Possible to take Options arg that overide / merge to defaults var monthNames = [ "January","February","March", "April","May","June","July", "August","September","October", "November","December" ]; var weekDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; var day = getFormattedDay(that.getDate(), zeroPaddedDay); var monthIndex = that.getMonth(); var month = getFormattedMonth(monthIndex + 1, zeroPaddedMnth); var year = that.getFullYear(); var wkDay = that.getDay(); var hour = getFormattedHour(that.getHours(), zeroPaddedHr); var minute = getFormattedMinute(that.getMinutes(), zeroPaddedMin); var meridian = getMeridian(that.getHours()); return { "day": day,"monthName": monthNames[monthIndex],"month": month, "weekDay": weekDays[wkDay],"year": year,"hour": hour,"minute": minute, "meridian": meridian }; }, compareDate: function (d2) { /// validates if caller is less than argument d2 = _isString(d2) ? new Date(d2) : d2; return !this.sameDate(d2) && typeof d2 !="number" ? that < d2 : false; }, sameDate: function (d) { return that.getFullYear() === d.getFullYear() && that.getDate() === d.getDate() && that.getMonth() === d.getMonth(); }, dateAfter: function (separator) { var fdate = this.formatDate(); var separator = separator ? separator :"-"; return fdate.year + separator + fdate.month + separator + (fdate.day + 1); } }; }; function _isString(obj) { var toString = Object.prototype.toString; return toString.call(obj) == '[object String]'; } function checkSeparator(separator) { // NOT GENERIC ... NEEDS REVISION switch (separator) { case"": sep = separator; break; case",": sep =" ,"; break; default: sep =""; break; } return sep; } function getFormattedHour(h, zeroPadded) { h = h % 12; h = h ? h : 12; // 12 instead of 00 return zeroPadded ? addZero(h) : h; } function getFormattedMinute(m, zeroPadded) { return zeroPadded ? addZero(m) : m; } function getFormattedDay(dd, zeroPadded) { return zeroPadded ? addZero(dd) : dd; } function getFormattedMonth(mm, zeroPadded) { return zeroPadded ? addZero(mm) : mm; } function getMeridian(hr) { return hr >= 12 ? 'PM' : 'AM'; } function addZero(i) { if (i < 10) { i ="0" + i; } return i; } Date.prototype.getFormattedDate = fnDateProcessor; } ()); |
你可以使用
但我们有一个很好的currently插件(parse,验证,的手法,显示的日期和时代moment.js使用JavaScript的村庄 </P >
这个答案的冰的人找一个约会与ISO 8601状格式和与时区。 它的纯制为那些谁不想include任何约会的图书馆。 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | var date = new Date(); var timeZone = date.toString(); //Get timezone ( 'GMT+0200' ) var timeZoneIndex = timeZone.indexOf('GMT'); //Cut optional string after timezone ( '(heure de Paris)' ) var optionalTimeZoneIndex = timeZone.indexOf('('); if(optionalTimeZoneIndex != -1){ timeZone = timeZone.substring(timeZoneIndex, optionalTimeZoneIndex); } else{ timeZone = timeZone.substring(timeZoneIndex); } //Get date with JSON format ( '2019-01-23T16:28:27.000Z' ) var formattedDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toJSON(); //Cut ms formattedDate = formattedDate.substring(0,formattedDate.indexOf('.')); //Add timezone formattedDate = formattedDate + ' ' + timeZone; console.log(formattedDate); |
我的一些东西,这样在控制台: </P >
2019-01-23T17:12:52 GMT+0100
jsfiddle:http:/ / / / 4 jsfiddle.net n9mszhjc / </P >
基础知识
如果您对
1 | var today = new Date(); |
date.prototype.toLocaledateString()。
如果要格式化输出,请考虑使用
1 2 3 4 5 6 | var today = new Date().toLocaleDateString('de-DE', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); |
如果您今天(1月24日)执行了该代码??,2016)在现代浏览器上,它将生成字符串
如果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | var DateObject = (function() { var monthNames = [ "January","February","March", "April","May","June","July", "August","September","October", "November","December" ]; var date = function(str) { this.set(str); }; date.prototype = { set : function(str) { var dateDef = str ? new Date(str) : new Date(); this.day = dateDef.getDate(); this.dayPadded = (this.day < 10) ? ("0" + this.day) :"" + this.day; this.month = dateDef.getMonth() + 1; this.monthPadded = (this.month < 10) ? ("0" + this.month) :"" + this.month; this.monthName = monthNames[this.month - 1]; this.year = dateDef.getFullYear(); } }; return date; })(); |
如果您包含该代码并在今天(1月24日)执行
1 2 3 4 5 6 | day: 24 dayPadded:"24" month: 1 monthPadded:"01" monthName:"January" year: 2016 |