Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript change date into format of (dd/mm/yyyy) [duplicate]

Tags:

javascript

How can I convert the following date format below (Mon Nov 19 13:29:40 2012)

into:

dd/mm/yyyy

<html>     <head>     <script type="text/javascript">       function test(){          var d = Date()          alert(d)       }     </script>     </head>  <body>     <input onclick="test()" type="button" value="test" name="test"> </body> </html> 
like image 557
user1451890 Avatar asked Nov 19 '12 18:11

user1451890


People also ask

How do I format a date in JavaScript?

const d = new Date("2015/03/25"); The behavior of "DD-MM-YYYY" is also undefined. Some browsers will try to guess the format. Some will return NaN.

How convert dd mm yyyy string to date in JavaScript?

To convert dd/mm/yyyy string into a JavaScript Date object, we can pass the string straight into the Date constructor. const dateString = "10/23/2022"; const dateObject = new Date(dateString);

How do you format a date mm dd yyyy?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.


1 Answers

Some JavaScript engines can parse that format directly, which makes the task pretty easy:

function convertDate(inputFormat) {    function pad(s) { return (s < 10) ? '0' + s : s; }    var d = new Date(inputFormat)    return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/')  }    console.log(convertDate('Mon Nov 19 13:29:40 2012')) // => "19/11/2012"
like image 182
maerics Avatar answered Sep 21 '22 07:09

maerics