国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

javascript - 我想在輸入數字自動格式化日期格式,格式化日期格式為:YYYY-MM-DD

瀏覽:134日期:2023-03-17 16:39:05

問題描述

我想寫一個組件,在輸入數字中比如:201705 自動格式化日期格式,最終日期格式為:2017-05 再次輸入06自動格式化2017-05-06

問題解答

回答1:

Edit: 對提出的 case 做簡單轉換:

const date str = ’20170523’const result = new Date()result.setFullYear(parseInt(str.substr(0, 4)))result.setMonth(parseInt(str.substr(4, 2)) - 1)result.setDate(parseInt(str.substr(6, 2)))// 函數定義見下getDateFromTimestamp(result.getTime())

一個簡單的實現如下:

// 1495517932472 毫秒級時間戳const date = new Date().getTime()function formatMonth (num) { return [ ’01’, ’02’, ’03’, ’04’, ’05’, ’06’, ’07’, ’08’, ’09’, ’10’, ’11’, ’12’ ][num]}function getDateFromTimestamp (ts) { const date = new Date(ts) const YYYY = date.getFullYear() const MM = formatMonth(date.getMonth()) const DD = date.getDate() return `${YYYY}-${MM}-${DD}`}// 2017-05-23console.log(getDateFromTimestamp(date))回答2:

You maybe need momentjs

回答3:

funtion format_num_to_date(num) { var str = String(num); if (str.length === 8) {var date = str.substr(0,4) + ’-’ + str.substr(4,2) + ’-’ + str.substr(6);return date; }}format_num_to_date(20170523);回答4:

//時間格式化// var time1 = new Date().Format(“yyyy-MM-dd”);// var time2 = new Date().Format(“yyyy-MM-dd HH:mm:ss”);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(), //秒 '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;}回答5:

let yyyymmdd = (date, sep = ’-’) => { return [date.getFullYear(),date.getMonth()+1,date.getDate() ].map(d => d.toString()) .map(d => (’00’ + d).slice(d.length >= 4 ? -4 : -2)).join(sep); }

javascript - 我想在輸入數字自動格式化日期格式,格式化日期格式為:YYYY-MM-DD

回答6:

原生<input type='date' pattern='[0-9]{4}-[0-9]{2}-[0-9]{2}'>不好嗎?

標簽: JavaScript
主站蜘蛛池模板: 武威市| 孟州市| 报价| 呈贡县| 平遥县| 芜湖县| 水城县| 烟台市| 大姚县| 吴江市| 五指山市| 离岛区| 罗源县| 龙井市| 霸州市| 荔波县| 贺州市| 泗水县| 和平区| 米林县| 民县| 图们市| 秦安县| 连平县| 随州市| 新巴尔虎右旗| 安图县| 康保县| 全南县| 兴文县| 荥阳市| 海口市| 西藏| 正安县| 郧西县| 双柏县| 正安县| 长治县| 北宁市| 肥东县| 始兴县|