码迷,mamicode.com
首页 > Web开发 > 详细

js 格式化string日期格式

时间:2015-05-05 21:13:42      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

<script type="text/javascript">
/**
 * string 转时间格式;
 */
  
function formatDate(date, format) {   
    if (date.length==0) return;   
    if (!format) format = "yyyy-MM-dd";   
    switch(typeof date) {   
        case "string":   
            date = new Date(date.replace(/-/, "/"));   
            break;   
        case "number":   
            date = new Date(date);   
            break;   
    }    
    if (!date instanceof Date) return;   
    var dict = {   
        "yyyy": date.getFullYear(),   
        "M": date.getMonth() + 1,   
        "d": date.getDate(),   
        "H": date.getHours(),   
        "m": date.getMinutes(),   
        "s": date.getSeconds(),   
        "MM": ("" + (date.getMonth() + 101)).substr(1),   
        "dd": ("" + (date.getDate() + 100)).substr(1),   
        "HH": ("" + (date.getHours() + 100)).substr(1),   
        "mm": ("" + (date.getMinutes() + 100)).substr(1),   
        "ss": ("" + (date.getSeconds() + 100)).substr(1)   
    };       
    return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {   
        return dict[arguments[0]];   
    });                   
}   
  


$(function(){
	
	
	$("#otherPublishDate").html(formatDate("2010-04-30", "yyyy年MM月dd"));
	
	});
</script>

 

js 格式化string日期格式

标签:

原文地址:http://www.cnblogs.com/hausthy/p/4480014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!