Formatting the Date in JavaScript

前端 未结 6 1668
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 06:54

Using newDate() function in Java script, I am able to get today\'s date. I am getting the date in the format 3/3/2009 (d/m/yyyy). But i actually need the date in the format

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-16 07:26

    If you want to roll-your-own, which is not too difficult, you can use the built-in javascript Date Object methods.

    For example, to get the current date in the format you want, you could do:

    var myDate = new Date();
    var dateStr = myDate.getFullYear + 
        '-' + (myDate.getMonth()+1) + '-' + myDate.getDate();
    

    You may need to zero-pad the getDate() method if you require the two-digit format on the day.

    I create a few useful js functions for date conversions and use those in my applications.

提交回复
热议问题