search

Home  >  Q&A  >  body text

javascript - I want to automatically format the date when entering numbers. The formatted date is: YYYY-MM-DD

I want to write a component that automatically formats the date when inputting numbers, such as: 201705. The final date format is: 2017-05. Enter 06 again to automatically format 2017-05-06

伊谢尔伦伊谢尔伦2754 days ago1196

reply all(6)I'll reply

  • 習慣沉默

    習慣沉默2017-07-05 10:52:15

    Edit: Simple conversion of the proposed 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())

    A simple implementation is as follows:

    // 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-23
    console.log(getDateFromTimestamp(date))

    reply
    0
  • 世界只因有你

    世界只因有你2017-07-05 10:52:15

    You maybe need momentjs

    reply
    0
  • 某草草

    某草草2017-07-05 10:52:15

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

    reply
    0
  • 怪我咯

    怪我咯2017-07-05 10:52:15

    //时间格式化
    // 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., (this.getFullYear() + "").substr(4 - RegExp..length));
      for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp., (RegExp..length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      return fmt;
    }

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-05 10:52:15

    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); 
    }

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-05 10:52:15

    Native<input type="date" pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}">Isn’t it bad?

    reply
    0
  • Cancelreply