How to format date Apr 17 1995 12:00:000AM for 1995-04-17 in java script?

Asked

Viewed 27 times

-2

I’m pulling a date straight from Sqlserver, but this returning me in this format Apr 17 1995 12:00:000AM is possible to create a function that returns me this format 1995-17-04 with js?

1 answer

1


Creates a function to format the date.
Handling dates in Javascript can be a bit boring, so you can do so:

function formatDate(sqlDate) {
  var date = new Date(sqlDate.substring(0, 11)),
      year = date.getFullYear(),
      month = String(date.getMonth() + 1).padStart(2, '0'),
      day = String(date.getDate()).padStart(2, '0');

  return year + '-' + month + '-' + day;
}
  • Thank you very much Leonardo!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.