列表

详情


FED27. 时间格式化输出

描述

按所给的时间格式输出指定的时间
格式说明
对于 2014.09.05 13:14:20
yyyy: 年份,2014
yy: 年份,14
MM: 月份,补满两位,09
M: 月份, 9
dd: 日期,补满两位,05
d: 日期, 5
HH: 24制小时,补满两位,13
H: 24制小时,13
hh: 12制小时,补满两位,01
h: 12制小时,1
mm: 分钟,补满两位,14
m: 分钟,14
ss: 秒,补满两位,20
s: 秒,20
w: 星期,为 ['日', '一', '二', '三', '四', '五', '六'] 中的某一个,本 demo 结果为 五

示例1

输入:

formatDate(new Date(1409894060000), 'yyyy-MM-dd HH:mm:ss 星期w')

输出:

2014-09-05 13:14:20 星期五

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 863ms, 内存消耗: 77864KB, 提交时间: 2020-12-02

{"css":"","js":"function formatDate(t,str){\n  var obj = {\n    yyyy:t.getFullYear(),\n    yy:(\"\"+ t.getFullYear()).slice(-2),\n    M:t.getMonth()+1,\n    MM:(\"0\"+ (t.getMonth()+1)).slice(-2),\n    d:t.getDate(),\n    dd:(\"0\" + t.getDate()).slice(-2),\n    H:t.getHours(),\n    HH:(\"0\" + t.getHours()).slice(-2),\n    h:t.getHours() % 12,\n    hh:(\"0\"+t.getHours() % 12).slice(-2),\n    m:t.getMinutes(),\n    mm:(\"0\" + t.getMinutes()).slice(-2),\n    s:t.getSeconds(),\n    ss:(\"0\" + t.getSeconds()).slice(-2),\n    w:['日', '一', '二', '三', '四', '五', '六'][t.getDay()]\n  };\n  return str.replace(/([a-z]+)/ig,function($1){return obj[$1]});\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 865ms, 内存消耗: 77772KB, 提交时间: 2020-12-26

{"css":"","js":"function formatDate(t,str){\n  var obj = {\n    yyyy:t.getFullYear(),\n    yy:(\"\"+ t.getFullYear()).slice(-2),\n    M:t.getMonth()+1,\n    MM:(\"0\"+ (t.getMonth()+1)).slice(-2),\n    d:t.getDate(),\n    dd:(\"0\" + t.getDate()).slice(-2),\n    H:t.getHours(),\n    HH:(\"0\" + t.getHours()).slice(-2),\n    h:t.getHours() % 12,\n    hh:(\"0\"+t.getHours() % 12).slice(-2),\n    m:t.getMinutes(),\n    mm:(\"0\" + t.getMinutes()).slice(-2),\n    s:t.getSeconds(),\n    ss:(\"0\" + t.getSeconds()).slice(-2),\n    w:['日', '一', '二', '三', '四', '五', '六'][t.getDay()]\n  };\n  return str.replace(/([a-z]+)/ig,function($1){return obj[$1]});\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 869ms, 内存消耗: 77868KB, 提交时间: 2020-11-22

{"css":"","js":"function formatDate(t,str){\n  var obj = {\n    yyyy:t.getFullYear(),\n    yy:(\"\"+ t.getFullYear()).slice(-2),\n    M:t.getMonth()+1,\n    MM:(\"0\"+ (t.getMonth()+1)).slice(-2),\n    d:t.getDate(),\n    dd:(\"0\" + t.getDate()).slice(-2),\n    H:t.getHours(),\n    HH:(\"0\" + t.getHours()).slice(-2),\n    h:t.getHours() % 12,\n    hh:(\"0\"+t.getHours() % 12).slice(-2),\n    m:t.getMinutes(),\n    mm:(\"0\" + t.getMinutes()).slice(-2),\n    s:t.getSeconds(),\n    ss:(\"0\" + t.getSeconds()).slice(-2),\n    w:['日', '一', '二', '三', '四', '五', '六'][t.getDay()]\n  };\n  return str.replace(/([a-z]+)/ig,function($1){return obj[$1]});\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 870ms, 内存消耗: 77772KB, 提交时间: 2020-12-29

{"css":"","js":"function formatDate(t,str){\n    var obj = {\n    yyyy:t.getFullYear(),\n    yy:(\"\"+ t.getFullYear()).slice(-2),\n    M:t.getMonth()+1,\n    MM:(\"0\"+ (t.getMonth()+1)).slice(-2),\n    d:t.getDate(),\n    dd:(\"0\" + t.getDate()).slice(-2),\n    H:t.getHours(),\n    HH:(\"0\" + t.getHours()).slice(-2),\n    h:t.getHours() % 12,\n    hh:(\"0\"+t.getHours() % 12).slice(-2),\n    m:t.getMinutes(),\n    mm:(\"0\" + t.getMinutes()).slice(-2),\n    s:t.getSeconds(),\n    ss:(\"0\" + t.getSeconds()).slice(-2),\n    w:['日', '一', '二', '三', '四', '五', '六'][t.getDay()] \n    };\n    return str.replace(/([a-z]+)/ig,function($1){return obj[$1]})\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 871ms, 内存消耗: 77808KB, 提交时间: 2021-02-21

{"css":"","js":"function formatDate(date,format){\n    var weeks = [\n        {\n            day: 1,\n            text: \"一\"\n        },\n        {\n            day: 2,\n            text: \"二\"\n        },\n        {\n            day: 3,\n            text: \"三\"\n        },\n        {\n            day: 4,\n            text: \"四\"\n        },\n        {\n            day: 5,\n            text: \"五\"\n        },\n        {\n            day: 6,\n            text: \"六\"\n        },\n        {\n            day: 7,\n            text: \"七\"\n        }\n    ]\n    var year = format.indexOf(\"yyyy\")!=-1 ? date.getFullYear() : date.getFullYear().toString().substr(2,3)\n    format = format.indexOf(\"yyyy\")!=-1 ? format.replace(\"yyyy\",year) : format.replace(\"yy\",year)\n    var month = date.getMonth() + 1;\n    month = format.indexOf(\"MM\")!=-1&&month < 10 ? \"0\" + month : month;\n    format = format.indexOf(\"MM\")!=-1?format.replace(\"MM\",month):format.replace(\"M\",month)\n    var day = date.getDate();\n    day = format.indexOf(\"dd\")!=-1&&day < 10 ? \"0\" + day : day;\n    format = format.indexOf(\"dd\")!=-1?format.replace(\"dd\",day):format.replace(\"d\",day)\n    var hour = date.getHours();\n    hour = (format.indexOf(\"HH\")!=-1 || format.indexOf(\"hh\")!=-1)&&hour < 10 ? \"0\" + hour : hour;\n    format = format.indexOf(\"HH\")!=-1?format.replace(\"HH\",hour):format.replace(\"H\",hour)\n    format = format.indexOf(\"hh\")!=-1?format.replace(\"hh\",hour):format.replace(\"h\",hour)\n    var minute = date.getMinutes();\n    minute = format.indexOf(\"mm\")!=-1&&minute < 10 ? \"0\" + minute : minute;\n    format = format.indexOf(\"mm\")!=-1?format.replace(\"mm\",minute):format.replace(\"m\",minute)\n    var second = date.getSeconds();\n    second = format.indexOf(\"ss\")!=-1&&second < 10 ? \"0\" + second : second;\n    format = format.indexOf(\"ss\")!=-1 ? format.replace(\"ss\",second) : format.replace(\"s\",second)\n    var weekDay = date.getDay();\n    var week = weeks.find(function(item){\n        return item.day === weekDay\n    }).text\n    format = format.replace(\"星期w\",\"星期\"+week)\n    return format\n}","html":"","libs":[]}

上一题