0

I have a string variable I want the variable into date format.

My Code:

var date = '2/3/2022 17:57:30'
var temp = new Date(date)

My Output:

2022-02-03T17:57:30.000Z

Expected Output:

2022-02-03

   
3
  • 1
    you can refer to this document stove99.tistory.com/46 Commented Mar 21, 2022 at 6:08
  • 1
    Hi, you can use the other date function to get the day and month and year and concat it with a string.temp will heave other built in date functions like temp.getFullYear() or temp.getMonth() and temp.getDate(). Get those and just convert it to string. make a helper function that will convert date into this format. Hope it helps Commented Mar 21, 2022 at 6:11
  • Is that Feb 3 or Mar 2? Commented Mar 21, 2022 at 6:30

2 Answers 2

1

You can use toLocaleDateString with an argument that specifies a locale for which your format is used. For instance, the Swedisch locale uses the YYYY-MM-DD format:

var date = '2/3/2022 17:57:30'
var temp = new Date(date).toLocaleDateString("se");
console.log(temp);

Sign up to request clarification or add additional context in comments.

Comments

1

There's no built-in method to convert date into your expected output although you can try this piece of code to convert date into your expected output;

var date = '2/3/2022 17:57:30'
var temp = new Date(date).toLocaleDateString().replaceAll("/","-");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.