Converting the sql date to MM/DD/YYYY

229 Views Asked by At

the timestamp I receive from Api is something like 2018-02-24T00:00:00 , but what I need is MM/DD/YYYY so.. it would look something like 02/24/2018, I receive timestamp from mssql and want to convert it using javascript

2

There are 2 best solutions below

0
On
SELECT FORMAT (timestamp, 'MM/dd/yyyy') as date
GO

or as US culture

SELECT FORMAT (timestamp, 'd', 'en-us') as date
GO
0
On

You can try:

function getFormattedDate(date) {
    let year = date.getFullYear();
    let month = (1 + date.getMonth()).toString().padStart(2, '0');
    let day = date.getDate().toString().padStart(2, '0');
  
    return `${month}/${day}/${year}`;
}

function getFormattedDate(date) {
    let year = date.getFullYear();
    let month = (1 + date.getMonth()).toString().padStart(2, '0');
    let day = date.getDate().toString().padStart(2, '0');
  
    return `${month}/${day}/${year}`;
}

console.log(getFormattedDate(new Date('2018-02-24T00:00:00')))