js 格式化时间

Date.prototype.format = function(format) {
       var date = {
              "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+)/i.test(format)) {
              format = format.replace(RegExp.$1, (this.getFullYear() + ‘‘).substr(4 - RegExp.$1.length));
       }
       for (var k in date) {
              if (new RegExp("(" + k + ")").test(format)) {
                     format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
              }
       }
       return format;
}
console.log(new Date().format(‘yyyy-MM-dd h:m:s‘));//2018-03-28 22:03

原文地址:https://www.cnblogs.com/xiangsj/p/8666301.html

时间: 2024-08-01 22:33:33

js 格式化时间的相关文章

js 格式化时间日期函数小结

下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下. 代码如下: Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(

js 格式化时间/Date(1425027069000)/格式化为yyyy-MM-dd HH:mm:ss

//格式化时间[参数是一个字符串,格式类似于 /Date(1425027069000)/ ] function formatDate(n) { var s = n.slice(6, 21); dt = new Date(parseInt(n.slice(6, 21))); var year = dt.getFullYear();; var month = dt.getMonth() + 1; var date = dt.getDate(); var hour = dt.getHours(); v

js格式化时间

Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds()

JS格式化时间(支持小程序,兼容IOS)

const REGEX = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/export const formatTime = (val, format)=>{ if (val) { /** * @instructions 如果不是时间戳格式,且含有字符 '-' 则将 '-' 替换成 '/' && 删除小数点及后面的数字 * @reason 将 '-' 替换成 '/' && 删除小数点及后面的数字 的原因是safari

js 格式化时间日期

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()

js 格式化时间毫秒

//格式华日期为2017-12-20 function datefomate(value) { if(value==null || value == undefined){ return ""; } var date = new Date(value); Y = date.getFullYear(), m = date.getMonth()+1, d = date.getDate(), H = date.getHours(), i = date.getMinutes(), s = da

js/jq基础(日常整理记录)-1-纯js格式化时间

一.纯js格式化时间 之前记录了一些,工作中发现的比较常用的使用,就记录一下. 由于很基础,就直接贴出来了,不做分析了. 改造一下Date的原型 Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "

js中格式化时间字符串

.net 程序员肯定有遇到过,将一个对象json序列化之后Date 字段 就会转化成 '/Date(1370770323740)/' 这种格式的数据,下面介绍一种在js中,关于时间格式的转换. <script> function formatDate(date, format) { if (!date) return; if (!format) format = "yyyy-MM-dd"; switch(typeof date) { case "string&qu

js -- 日期时间格式化

/** * js日期时间格式化 * @param date 时间读对象 * @param format 格式化字符串 例如:yyyy年MM月dd日 hh时mm分ss秒 * @returns {string} 返回格式化后的字符串 */function dateFormat (date, format) { var o = { "M+": date.getMonth() + 1, //month "d+": date.getDate(), //day "h+