Converting date string from dashes to forward slashes

前端 未结 3 1773
长情又很酷
长情又很酷 2021-01-25 04:57

I am trying to convert my dashed date 2013-12-11 to 2013/12/11 using the following function:

function convertDate(stringdate)
{
    // Internet Explorer does not         


        
相关标签:
3条回答
  • 2021-01-25 05:37

    Your function is working as expected convertDate(myDate) is returning the / value off the date.

    Your problem seems to be your logging

    var myDate = '2013-12-11';
    console.log('Before', myDate); //2013-12-11
    
    convertDate(myDate);
    console.log('After', myDate); //2013-12-11 
    

    Your function returns a value so convertDate(myDate) is just returning and doing nothing. And your console log for after is just returning the same date as before.

    If you change your console log to

    console.log('After', convertDate(myDate)); //2013-12-11 
    

    You will get the expected result, or set myDate to the new value

      myDate = convertDate(myDate);
        console.log('After', myDate); //2013-12-11 
    
    0 讨论(0)
  • 2021-01-25 05:42

    I'm not sure if I misunderstand the question; why not just this:

    function convertDate(stringdate)
    {
        stringdate = stringdate.replace(/-/g, "/");
        return stringdate;
    }
    
    0 讨论(0)
  • 2021-01-25 06:01

    Use String Replace to replace the dashes with slashes.

    string.replace(/-/g,"/")
    
    0 讨论(0)
提交回复
热议问题