Javascript add leading zeroes to date

后端 未结 25 1816
执笔经年
执笔经年 2020-11-22 02:50

I\'ve created this script to calculate the date for 10 days in advance in the format of dd/mm/yyyy:

var MyDate = new Date();
var MyDateString = new Date();
M         


        
相关标签:
25条回答
  • 2020-11-22 03:36

    Try this: http://jsfiddle.net/xA5B7/

    var MyDate = new Date();
    var MyDateString;
    
    MyDate.setDate(MyDate.getDate() + 20);
    
    MyDateString = ('0' + MyDate.getDate()).slice(-2) + '/'
                 + ('0' + (MyDate.getMonth()+1)).slice(-2) + '/'
                 + MyDate.getFullYear();
    

    EDIT:

    To explain, .slice(-2) gives us the last two characters of the string.

    So no matter what, we can add "0" to the day or month, and just ask for the last two since those are always the two we want.

    So if the MyDate.getMonth() returns 9, it will be:

    ("0" + "9") // Giving us "09"
    

    so adding .slice(-2) on that gives us the last two characters which is:

    ("0" + "9").slice(-2)
    "09"
    

    But if MyDate.getMonth() returns 10, it will be:

    ("0" + "10") // Giving us "010"
    

    so adding .slice(-2) gives us the last two characters, or:

    ("0" + "10").slice(-2)
    "10"
    
    0 讨论(0)
  • 2020-11-22 03:37

    As @John Henckel suggests, starting using the toISOString() method makes things easier

    const dateString = new Date().toISOString().split('-');
    const year = dateString[0];
    const month = dateString[1];
    const day = dateString[2].split('T')[0];
    
    console.log(`${year}-${month}-${day}`);

    0 讨论(0)
  • 2020-11-22 03:38
     let date = new Date();
     let dd = date.getDate();//day of month
    
     let mm = date.getMonth();// month
     let yyyy = date.getFullYear();//day of week
     if (dd < 10) {//if less then 10 add a leading zero
         dd = "0" + dd;
       }
     if (mm < 10) {
        mm = "0" + mm;//if less then 10 add a leading zero
      }
    
    0 讨论(0)
  • 2020-11-22 03:39

    I found the shorterst way to do this:

     MyDateString.replace(/(^|\D)(\d)(?!\d)/g, '$10$2');
    

    will add leading zeros to all lonely, single digits

    0 讨论(0)
  • 2020-11-22 03:41

    You can use String.slice() which extracts a section of a string and returns it as a new string, without modifying the original string:

    const currentDate = new Date().toISOString().slice(0, 10) // 2020-04-16
    

    Or you can also use a lib such as Moment.js to format the date:

    const moment = require("moment")
    const currentDate = moment().format("YYYY-MM-DD") // 2020-04-16
    
    0 讨论(0)
  • 2020-11-22 03:44

    Another option, using a built-in function to do the padding (but resulting in quite long code!):

    myDateString = myDate.getDate().toLocaleString('en-US', {minimumIntegerDigits: 2})
      + '/' + (myDate.getMonth()+1).toLocaleString('en-US', {minimumIntegerDigits: 2})
      + '/' + myDate.getFullYear();
    
    // '12/06/2017'
    

    And another, manipulating strings with regular expressions:

    var myDateString = myDate.toISOString().replace(/T.*/, '').replace(/-/g, '/');
    
    // '2017/06/12'
    

    But be aware that one will show the year at the start and the day at the end.

    0 讨论(0)
提交回复
热议问题