Date.prototype.format =function(format)
    {
        var o = {
        "M+" : (this.getMonth()+1)+"", //month
   "d+" : this.getDate()+"",    //day
     "h+" : this.getHours()+"",   //hour
   "m+" : this.getMinutes()+"", //minute
   "s+" : this.getSeconds()+"", //second
     "q+" : Math.floor((this.getMonth()+3)/3)+"",  //quarter
   "S" : this.getMilliseconds() //millisecond+""
        }
        if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
        (this.getFullYear()+"").substr(4- RegExp.$1.length));
 
        for(var k in o)if(new RegExp("("+ k +")").test(format))
        format = format.replace(RegExp.$1,
        RegExp.$1.length==1? o[k] :("00"+ o[k]).substr(o[k].length));
        return format;
    }
以上代码必须先声明,然后在使用。使用方法:
var d =new Date().format('yyyy-MM-dd');

另一种方法:

在Javascript之中,日期对象是Date,那么如何将一个日期对象按照定制的格式进行输出呢? 
  可以现告诉你,Date对象有有四个内置方法,用于输出为字符串格式,分别为: 
  1)toGMTString,将一个日期按照GMT格式显示 
  2)toLocaleString,将一个日期按照本地操作系统格式显示 
  3)toLocaleDateString,按照本地格式显示一个日期对象的日期部分 
  4)toLocaleTimeString,按照本地格式显示一个日期对象的时间部分

用JavaScript(js)对时间格式化的更多相关文章

  1. js Date 时间格式化的扩展

    js Date 时间格式化的扩展: Date.prototype.format = function (fmt) { var o = { , //月 "d+": this.getD ...

  2. js -- 日期时间格式化

    /** * js日期时间格式化 * @param date 时间读对象 * @param format 格式化字符串 例如:yyyy年MM月dd日 hh时mm分ss秒 * @returns {stri ...

  3. Javascript 金额、时间格式化

    一晃2017年已经过去了,2018年已经悄然而至.回首过去的2017年,工作还是一如既往,但生活却有了翻天覆地的变化.尚还觉得自己还小的自己,在过去的一年中却完成了两件人生大事,回想起来还是一脸懵逼, ...

  4. js 中时间格式化的几种方法

    1.项目中时间返回值,很过时候为毫秒值,我们需要转换成 能够看懂的时间的格式: 例如: yyyy-MM-dd HH:mm:ss 2.处理方法(处理方法有多种,可以传值到前端处理,也可以后台可以好之后再 ...

  5. JS 自定义时间格式化

    // 对Date的扩展,将 Date 转化为指定格式的String// 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位 ...

  6. js本地时间格式化

    var myDate = new Date(); //获取当前时间及日期 var year=myDate.getYear(); // 获取当前年份(当前年份-1900) var fyear=myDat ...

  7. js获取时间格式化

    http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html

  8. JS日期时间格式化

    Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...

  9. JS Date 时间格式化

    Date2Str(x, y) { , d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s: x.getSeconds() }; y = y.re ...

随机推荐

  1. Trie,HDU1875world puzzle

    附上代码 #include<iostream> #include<cstdio> #include<string> #include<cstring> ...

  2. hdu 1753 大明A+B(高精度小数加法)

    //深刻认识到自己的粗心,为此浪费了一天.. Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是 ...

  3. Redhat 6.5 x64 下载地址

    http://ftp.okhysing.is/ftp/redhat/6.5/isos/x86_64/

  4. 如何使用命令提示符进入mysql

    如果mysql安装时的路径不是在C盘,应进入mysql的bin目录中,然后在命令提示符中输入“mysql -u USERNAME -pPASSWORD ” 如果如果mysql安装时的路径是在C盘,直接 ...

  5. PHP学习心得(九)——函数

    一个函数可由以下的语法来定义.任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和类定义. 函数名和 PHP 中的其它标识符命名规则相同.有效的函数名以字母或下划线打头,后面跟字母,数字 ...

  6. centos不能挂在ntfs

    root@s 下载]# mount /dev/sdb1 /mnt mount: unknown filesystem type 'ntfs' wget http://www.tuxera.com/co ...

  7. Python 的格式化字符串format函数

    阅读mattkang在csdn中的博客<飘逸的python - 增强的格式化字符串format函数>所做笔记 自从python2.6开始,新增了一种格式化字符串的函数str.format( ...

  8. hdu 5648 DZY Loves Math 组合数+深搜(子集法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...

  9. Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值

    The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...

  10. MVC-简单验证码制作

    1.制作验证码: using System; using System.Collections.Generic; using System.Drawing; using System.Drawing. ...