13

I need to convert a date like this, 03/07/2016 to a date format like, 2016-03-07.

How can I do this using javascript or jquery?

Jordan Davis
  • 855
  • 3
  • 15
  • 22
  • FYI, jQuery *is* Javascript. It's just a library. – Mike Cluck Mar 07 '16 at 23:21
  • 2
    If your input is a string then this is easiest with string methods rather than using Date objects. You could, e.g., use .slice() along with concatenation, or do it in one line with a regex .replace(). What have you tried? – nnnnnn Mar 07 '16 at 23:22
  • If you want to use complex logic to work with dates, it is better to use special library such as `momentjs`. – Dmitriy Mar 07 '16 at 23:25
  • 1
    complex logic like turning ABC into CAB? yeah, better load KBs of code for that... – dandavis Mar 07 '16 at 23:28
  • The output format in the question title does *not* match the example in the question body. Please [edit] your question to fix whichever is wrong. – nnnnnn Mar 07 '16 at 23:31
  • corrected title of question, my mistake – Jordan Davis Mar 07 '16 at 23:38
  • Here is the answer [enter link description here](http://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd) – Siraj Hussain Mar 07 '16 at 23:44
  • [This is what you are looking](http://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd) Is JsFiddler you can put you own date formate to test. – Siraj Hussain Mar 07 '16 at 23:46

5 Answers5

24

Assuming your input is a string, this is easy to do using a regular expression with the String .replace() method:

var input = "03/07/2016";
var output = input.replace(/(\d\d)\/(\d\d)\/(\d{4})/, "$3-$1-$2");

Actually, if the input format is guaranteed, you could just swap the pieces around based on their position without bothering to explicitly match digits and forward slashes:

var output = input.replace(/(..).(..).(....)/, "$3-$1-$2");
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
11

Use the split, reverse and join functions:

var yourdate = date.split("/").reverse().join("-");

The split will split the date in various parts, with / as a delimiter. The reverse function will reverse all the parts in the array, generated by the split. The join function will join all the parts back together, but now with - as a delimiter.

Edit

After reading the comments about the date being out of order: swap the second and third values of the array, created by the split function.

var dat = "03/07/2016"
var yourdate = dat.split("/").reverse();
var tmp = yourdate[2];
yourdate[2] = yourdate[1];
yourdate[1] = tmp;
yourdate = yourdate.join("-");
CPUFry
  • 566
  • 4
  • 18
  • 1
    That will put the day and month in the wrong order. [Edit:] Sorry, this *does* work for the format specified in the question title, but the example in the question body is different. I don't know which the OP wants. – nnnnnn Mar 07 '16 at 23:26
  • @Dries VB this answer returns 2016-07-03. I need 2016-03-07. – Jordan Davis Mar 07 '16 at 23:27
8

If you want to avoid using regular expressions, manipulating strings, or loading an entire library like moment, you can do this in one line by creating a new JavaScript Date object with the date you want formatted and calling its toLocaleDateString method with a compatible locale, such as fr-CA.

new Date('01/31/2020').toLocaleDateString('fr-CA') // "2020-01-31"

Supported in all modern browsers.


Credit: https://masteringjs.io/tutorials/fundamentals/date_format

Ian Harris
  • 81
  • 1
  • 3
1
const date = new Date();
const convertedDate = date.toLocalDateString('en-IN').split('/').reverse().join('-');

console.log(convertedDate);
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

Please follow the below method. The trick is to use the unshift property.

function formatDate(__d){
    if(__d.indexOf('/')){
      var a = __d.split('/')
      var b = a.pop()
      a.unshift(b)
   }
  return a.join('-')
}