在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ JS格式化日期

JS格式化日期

2018/7/26格式化成2018-07
怎么寫啊

回答
編輯回答
祉小皓

2018-06-20 16:40 轉(zhuǎn)換
Date.prototype.Format = function(fmt) {
var 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 (var 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;
};
//使用
var testTimeB = '2018-06-20 16:40';
testTimeB = testTimeB.replace(/-/g, "/"); // ios 兼容時間 將-替換為/
var timeB = new Date(testTimeB).Format('yyyy/MM/dd hh:mm');

2017年10月6日 00:05
編輯回答
玩控

瀉藥,只是面對這個題目的話:
('2018/7/26').replace(/\b(\d)\b/g, '0$1').replace(/^(\d{4})\/(\d{2})\/\d{2}/,'$1-$2')


分兩步操作:

  1. 替換日期字符中的單個位數(shù)為雙位數(shù),包括月份和天
  2. 替換 /-

其中,$1 $2 是分組操作,代表正則中()中的匹配內(nèi)容,$1就是第一個括號中的$2就是第二個括號中的,如果存在嵌套,那么從外向里數(shù)。

2018年8月2日 13:23
編輯回答
維她命
function leftpad  (str, len, ch) {
  // str:要轉(zhuǎn)換的字符串/數(shù)字、len:轉(zhuǎn)多長、ch:拼接符
  str = String(str)
  var i = -1
  if (!ch && ch !== 0) ch = ' '
  len = len - str.length
  while (++i < len) {
    str = ch + str
  }
  return str
}

function revertDate(date){
    var str = date.split('/');
    var res = str[0] + '-' + leftpad(str[1], 2, '0');
    return res;
}
console.log(revertDate('2018/7/26'));
2018年1月18日 04:33
編輯回答
朕略傻
var a = '2018/7/26'.split('/');
var b = a[0] + '-' + (a[1] < 10 ? '0':'') + a[1];
console.log(b);
2017年9月23日 23:08