0

I'm trying to change the date format in javascript where in a variable i m getting the date value like this "11/09/2019" and want to change in a format like this "11-09-2019".but somehow i'm getting this format "sat nov 09 2019" whereas it should be "tue sep 11 2019".

can anyone help me. any help would be appreciated.

var alt_date1 = "11/09/2019";

var date = new Date(alt_date1);

alert(date);

output: Sat Nov 09 2019 00:00:00 GMT+0530 (India Standard Time)

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
user6891871
  • 164
  • 1
  • 3
  • 17

2 Answers2

0

If I understand your question right, you want a different format i.e., "-" instead of "/" and you're also getting the wrong date? 2 days ahead? Depending on your environment (browser, node, etc) the Date API can behave differently. I think this link might help you: JavaScript's getDate returns wrong date (Not sure though, some answers pertain to the time zones it uses)

As for formatting look here: https://codehandbook.org/javascript-date-format/

Example from that site:

let current_datetime = new Date()
let formatted_date = current_datetime.getDate() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getFullYear()
console.log(formatted_date)

// output
"19-10-2018"
Ale8k
  • 153
  • 1
  • 12
0

It's a little bit tricky to understand the question fully, but if you don't need a Date object, and you want to just format the string as 11-09-2019 it can be achieved with the simple replace function like this:

alt_date1.replace(/\//g, '-')

Dima Mamchur
  • 1,104
  • 9
  • 8