Search code examples
javascriptexcelstringdateinteger

Javascript convert from string number to integer


I am creating an excel date format while I using JavaScript but I got stuck about convert string to integer.

function getCurrentDateFormat (date,errorValue, removeSecond, clientTimeZoneOffset) {

    if (date !== undefined && date !== null) {
        const currDate = new Date(date)

        const currDay = currDate.getDate()
        const currMonth = currDate.getMonth()
        const currYear = currDate.getFullYear()
        const currHour = currDate.getHours().toString().padStart(2,0)
        const currMinutes = currDate.getMinutes()
        const currSeconds = currDate.getSeconds()

        return currDay + "/" + currMonth + 1 + "/" + currYear + " " + currHour + ":" + currMinutes + ":" + currSeconds
 
    }
    else {
        return errorValue
    }

}

This function give me this result : 30/01/2024 9:27:24

I want to make hour like 09. When I try to make integer zero(0) disappear. How can fix this situation?


Solution

  • Try the following

    var s = "0" + currHour;
    var hr= s.substr(s.length-2);
    
    return currDay + "/" + currMonth + 1 + "/" + currYear + " " + hr + ":" + 
    currMinutes + ":" + currSeconds;