How to format a JavaScript date(jquery ,json ,日期)
2015-11-11 19:07
911 查看
http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date方法1:
执行结果:11 November 2015方法二:If you need to quickly format your date using plain JavaScript, use
if you need it to be padded with zeros:
注: only work in Firefox. Both IE and Chrome are failing方法四:If you are already using jQuery UI in your project you could do it this way:
http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date方法1:
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var date = new Date(); var day = date.getDate(); var monthIndex = date.getMonth(); var year = date.getFullYear(); console.log(day, monthNames[monthIndex], year); document.write(day + ' ' + monthNames[monthIndex] + ' ' + year);
执行结果:11 November 2015方法二:If you need to quickly format your date using plain JavaScript, use
getDate,
getMonth + 1,
getFullYear,
getHoursand
getMinutes:
var d = new Date(); var datestring = d.getDate() + "-" + (d.getMonth()+1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes(); // 16-5-2015 9:50Or,
if you need it to be padded with zeros:
<span style="font-family:FangSong_GB2312;">var datestring = ("0" + d.getDate()).slice(-2) + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" + d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2); // 16-05-2015 09:50</span>方法三:use the non-standard Date method
toLocaleFormat(formatString)
var today = new Date(); today.toLocaleFormat('%d-%b-%Y'); // 30-Dec-2011
注: only work in Firefox. Both IE and Chrome are failing方法四:If you are already using jQuery UI in your project you could do it this way:
var formatted = $.datepicker.formatDate("M d, yy", new Date("2014-07-08T09:02:21.377")); // formatted will be 'Jul 8, 2014'注:https://jqueryui.com/resources/demos/datepicker/date-formats.html方法五:use the date format library 路径(http://blog.stevenlevithan.com/archives/date-time-format)
var dateFormat = require('dateformat'); var today = new Date(); dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"); returns: Saturday, June 9th, 2007, 5:46:21 PM注:可以使用的JS工具类信息.1)http://momentjs.com/2)http://www.datejs.com/2007/11/27/getting-started-with-datejs/3)href="http://www.javascriptsource.com/repository/javascripts/2009/03/880961/JS_Simple_Date_Format.zip"
相关文章推荐
- jquery获取屏幕尺寸方法
- 用纯JavaScript替代jQuery的技巧
- jquery实现当页面滚动超过一屏时显示返回顶部按钮
- AJAX+json+jquery实现预加载瀑布流布局
- JQuery 限制文本框只能输入数字和小数点
- jQuery对象长度size
- jquery 中ajax的使用
- jQuery.form提交复杂表单
- jQuery-1.9.1源码分析系列(三) Sizzle选择器引擎——一些有用的Sizzle API
- jquery 追加table
- jQuery通过滚轮滚动动态加载图片
- 多个jquery冲突
- 用jQuery.form实现批量输入控件的提交
- 35 个 jQuery 小技巧
- Rails项目中使用jQuery操作session
- jquery on事件jquery on实现绑定多个事件
- IE7下不支持jquery.attr("onclick","function(){}"),
- 配置JQuery与其它库的兼容性
- 很多人想要的Jquery 加载Loading效果,可以加到自己的项目中,结合Ajax使用
- jquery bind()与unbind()用法