// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
// let time1 = new Date().Format("yyyy-MM-dd");
// let time2 = new Date().Format("yyyy-MM-dd HH:mm:ss"); Date.prototype.Format = function (fmt) { //author: meizz
let o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (let k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};

  

Js 将 Date 转化为指定格式的String的更多相关文章

  1. JS对Date的扩展,将 Date 转化为指定格式的String

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

  2. 对Date的扩展,将 Date 转化为指定格式的String

    <script language="javascript" type="text/javascript"><!-- /** * 对Date的扩 ...

  3. 【JS】 JS毫秒值转化为正常格式 或者正常格式转化为毫秒值

    1.毫秒值转化为正常时间格式  最简单的方法 new Date(后台传来的毫秒值).toLocaleDateString() 就是这个样子 2.毫秒值转化为自定义的时间格式 本页面重写一下  toLo ...

  4. JS将JSON日期转换为指定格式的日期

    1.引入JS日期转换的函数库 function Format(now,mask) { var d = now; var zeroize = function (value, length) { if ...

  5. js中时间戳转化成时间格式

    function formatDate(timestamp){ var test = new Date(parseInt(timestamp) * 1000); var $year = test.ge ...

  6. JS 将数字转化成为货币格式

    最近由于项目的需要需要将数字format成货币格式,自己搞了半天效果不是很好,博客园有篇问题很好,再次转载记录一下 http://www.cnblogs.com/mingmingruyuedlut/a ...

  7. js将时间戳转化为日期格式

    function getLocalTime(nS) {        var date = new Date(nS);        var Y = date.getFullYear() + '-'; ...

  8. sql 将datetime类型转化为指定格式的字符串

    ), ) -- 输出: 05 16 2006 10:57AM ), ) -- 输出: 05/16/06 ), ) -- 输出: 06.05.16 ), ) -- 输出: 16/05/06 ), ) - ...

  9. web 上读取图片,并转化为指定格式

    一. 转换为 base64 public static string ObtainBase64FromWeb(string domain, string path) { string url = &q ...

随机推荐

  1. docker registry的https错误解决

    从docker1.3.2版本开始默认docker registry使用的是https,当你用docker pull 非https的docker regsitry的时候会报下面错误: Error: In ...

  2. 插件—jquery.validate.js

    前言 在学习jquery.validate.js中的一个小案例,只是这个插件的简单使用. 案例代码如下: <head>    <title></title>    ...

  3. Java使用BigDecimal解决浮点型运算丢失精度的问题

    @Test public void test1(){ System.out.print(0.05+0.01); } @Test public void test2(){ BigDecimal b1 = ...

  4. 03.Curator深入使用

    1.Apache Curator简介     Curator提供了一套Java类库,可以更容易的使用ZooKeeper.ZooKeeper本身提供了Java Client的访问类,但是API太底层,不 ...

  5. 160418、ztree权限菜单

    1.页面中引用ztree的css和js(大家自行下载ztree) <link rel="stylesheet" href="${ctx}/resources/js/ ...

  6. POJ 2374 Fence Obstacle Course(线段树+动态规划)

    Fence Obstacle Course Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2524   Accepted:  ...

  7. 【react 分页器】 基于react-virtualized组件的分页器

    react-virtualized 组件本身没有提供分页器功能,见这个issue:https://github.com/bvaughn/react-virtualized/issues/24 如果想给 ...

  8. golang: multiple http.writeHeader calls

    背景: golang的http服务,读取文件,提供给client下载时候. 出现 multiple http.writeHeader calls 错误. func DownloadFile(w htt ...

  9. FindBugs——帮助查找隐藏的bug

    FindBugs 1.什么是FindBugs FindBugs 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.有了静态分析工具,就可以在不实际运行程序 ...

  10. Spark Streaming实战

    1.Storm 和 SparkStreaming区别 Storm                      纯实时的流式处理,来一条数据就立即进行处理 SparkStreaming 微批处理,每次处理 ...