一个格式化日期和时间的JavaScript类库
原文地址:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html
结合meizz的代码做了适当调整。
Date.prototype.Format = function (fmt) {
var that = this;
var postfix = false;
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": function () {
return postfix ? (that.getHours() % 12 == 0 ? 12 : that.getHours() % 12) : that.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(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
if (RegExp.$1.length == 1 && RegExp.$1 == "h") postfix = true;
var val = typeof (o[k]) == "function" ? o[k].apply(this) : o[k];
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (val) : (("00" + val).substr(("" + val).length)));
}
}
return fmt + (postfix ? ((this.getHours() < 12) ? " AM" : " PM") : "");
}
// following are testing code
var Ld = "26-09-2014";
var Lt = "13:15:23";
var arrDateParts = Ld.split("-");
if (arrDateParts.length == 3) {
var dayNum = arrDateParts[0];
var monthNum = arrDateParts[1];
var yearNum = arrDateParts[2];
var D = new Date(monthNum.concat("/", dayNum, "/", yearNum, ' ', Lt));
console.info(D.Format("M/dd/yyyy h:mm:ss"));
}
以上代码可根据所提供的格式字符串以及时间来判断是否需要添加AM/PM后缀。
一个格式化日期和时间的JavaScript类库的更多相关文章
- Java 格式化日期、时间
有三种方法可以格式化日期.时间. 1.使用DateFormat类 获取DateFormat实例: DateFormat.getDateInstance() 只能格式化日期 2019年5 ...
- Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化
一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...
- Python中日期和时间格式化输出的方法
本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...
- Python 日期和时间_python 当前日期时间_python日期格式化
Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...
- java 使用simpleDateFormat格式化日期 时间.RP
首先了解一下格式化日志的所有表示. 时间日期标识符: yyyy:年 MM:月 dd:日 hh:1~12小时制(1-12) HH:24小时制(0-23) mm:分 ss:秒 S:毫秒 E:星期几 D:一 ...
- 使用moment.js轻松管理日期和时间
大家在前端Javascript开发中会遇到处理日期时间的问题,经常会拿来一大堆处理函数才能完成一个简单的日期时间显示效果.今天我给大家介绍一个轻量级的Javascript日期处理类库:moment.j ...
- python笔记7:日期和时间
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示. 时间 ...
- PHP日期与时间
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数.它也被称为 Unix 时间戳(Unix Timestamp).Unix时间戳(Unix timestamp),或称Uni ...
- 【转】Python 日期和时间
本文转自:http://www.runoob.com/python/python-date-time.html Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Pytho ...
随机推荐
- C#十种语法糖
语法糖 指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用.通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会.需要声明的是"语法糖" ...
- vnc
Xvnc, Xvnc-core, vncagent, vncinitconfig, vnclicense, vnclicensehelper, vnclicensewiz, vncpasswd, vn ...
- ( 解压缩版 免安装版 或 zip版 )如何修改mysql5.6.24 字符编码
1.当我们把zip文件格式解压到指定目录后,并且完成基本环境配置后,打开mysql 5.6.24会发现名为[my-default.ini]的文件.我们用记事本打开该文件会发现并没有[default-c ...
- 使用python的subprocess启动windows程序提示WindowsError: [Error 6] The handle is invalid
代码如下: subp = subprocess.Popen(cwd_path + "test.exe", cwd = cwd_path, shell = True, stdout ...
- Visio控件关闭“形状”面板
Visio.Window winShapeSearch = axDrawingControl1.Window.Windows.get_ItemFromID((int)Visio.VisWinTypes ...
- hibernate学习(设计多对多 关系 映射)
// package org.crazy.app.domain; import java.util.HashSet; import java.util.Set; import javax.persis ...
- Spring 通过FactoryBean配置Bean
1.实现FactoryBean接口 import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean ...
- div mouseenter 事件在IE下无效
解决方法是在div style上加个背景色: background: rgba(0, 0, 0, 0);filter:alpha(opacity=0); background: rgba(0, 0, ...
- canvas 画六边形
<section class="m1-c"> <div class="m1-t clearfix"> <ul> <li ...
- 手机驱动无法正常安装,出现adb interface失败
手机一直无法用usb连接上电脑,试了各种方法,总是提示安装驱动失败,或者找不到文件. 在网上找了各种方法,后来结果证明,是我自己手贱了,... 方法: Win7系统用户已经碰到几次在安装adb驱动时提 ...