How to convert a full date to a short date in javascript?

后端 未结 8 1749
余生分开走
余生分开走 2020-12-13 23:40

I have a date like this Monday, January 9, 2010

I now want to convert it to

1/9/2010 mm/dd/yyyy

I tried to do this

    var startDat         


        
8条回答
  •  轮回少年
    2020-12-13 23:45

    Here you go:

    (new Date()).toLocaleDateString('en-US');
    

    That's it !!

    you can use it on any date object

    let's say.. you have an object called "currentDate"

    var currentDate = new Date(); //use your date here
    currentDate.toLocaleDateString('en-US'); // "en-US" gives date in US Format - mm/dd/yy
    

    (or)

    If you want it in local format then

    currentDate.toLocaleDateString(); // gives date in local Format
    

提交回复
热议问题