8

I need the date to show in this format 2014-11-04 as "yy mm dd"

Currently, my script still shows me Tue Nov 04 2014 00:00:00 GMT+0200 (Egypt Standard Time)

$(document).ready(function() { 
    var userDate = '04.11.2014';
    from = userDate.split(".");
    f = new Date(from[2], from[1] - 1, from[0]);
    console.log(f); 
});
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Nasser
  • 159
  • 1
  • 2
  • 10

3 Answers3

18

You can construct this using the date object's methods

var date    = new Date(userDate),
    yr      = date.getFullYear(),
    month   = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(),
    day     = date.getDate()  < 10 ? '0' + date.getDate()  : date.getDate(),
    newDate = yr + '-' + month + '-' + day;
console.log(newDate);
Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • Thanks a lot but still not what i need , has to show zero if month or day is a single number – Nasser Oct 24 '14 at 14:46
  • Why is there a '+' sign before date.getMonth() and date.getDate()? Are these necessary? – Anthony Jan 07 '16 at 20:35
  • 1
    @Anthony that's the unary plus operator. Its used to coerce the operands to a number, e.g. `+"3" === 3`. And no, IIRC those functions return integers anyway, so no they're not necessary. – Jared Smith Jan 07 '16 at 21:21
  • just replace month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(), with month = (date.getMonth()+1) < 10 ? '0' + (date.getMonth()+1) : (date.getMonth()+1), – Arslan Tabassum Oct 25 '19 at 07:07
8

You may try the following:

   $(document).ready(function() {
        var userDate = '04.11.2014';
        var from = userDate.split(".");
        var f = new Date(from[2], from[1], from[0]);
        var date_string = f.getFullYear() + " " + f.getMonth() + " " + f.getDate();
        console.log(date_string);
    });

Alternatively I would look into Moment.js It would be way easier to deal with dates:

$(document).ready(function() {
    var userDate = '04.11.2014';
    var date_string = moment(userDate, "DD.MM.YYYY").format("YYYY-MM-DD");
    $("#results").html(date_string);
});

MOMENT.JS DEMO: FIDDLE

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • [.getMonth()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth) should return a `zero-based number` (means from 0 - 11) while yours returns `"11"` http://jsfiddle.net/8ke27xth/1/ `8D` – Roko C. Buljan Oct 24 '14 at 14:30
  • I understand how this is zero-based, just didnt seem to matter since he just wants the month output. In any case, moment.js is a way better approach. – CodeGodie Oct 24 '14 at 14:39
  • almost there thanks but for sure i'll need zero for example if month is March then show print 03 and if day fourth should print 04 so result should be 2014-03-04 , thanks – Nasser Oct 24 '14 at 14:45
  • @nasser ...no, zero based means 0-11 instead of 1-12 – Roko C. Buljan Oct 24 '14 at 14:49
  • with your code it prints 2014-11-4, but this is will confuse my search , it has to be 2014-11-04 so i need the zero to show if its a single number – Nasser Oct 24 '14 at 14:51
  • @nasser Can you not use moment.js? this will save you a lot of pain. I had to create a calendar in the past and I initially did not want to make the move but once I did, I never went back. Dont try to re-invent the wheel. – CodeGodie Oct 24 '14 at 14:52
0

I think you might find you answer here: Converting string to date in js

Replace the "." with "-" to validate the date.

Edit: this is done in javascript, Jquery does not have a utillity function for date

Community
  • 1
  • 1
ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
  • 1
    Are you refering to the datepicker? If so then Im sorry for missleading, i though he wanted a custom soloution that did not involve datepicker – ThunD3eR Oct 24 '14 at 14:27