0

I have this string:

3913-12-10T20:00:00+0000

How do I change it to dd/mm/yyyy format? My main problem is that I don't recognzie the pattern. It's actually 11/11/2013, but I don't get how to extract it.

Euphe
  • 3,531
  • 6
  • 39
  • 69
  • Where did you get this date string from? – gen_Eric Feb 26 '14 at 15:44
  • 3
    Doesn't look like 11/11/2013 to me. – Lightness Races in Orbit Feb 26 '14 at 15:45
  • `3913-12-10T20:00:00+0000` how did this data is generated? I would be nice if you share that code too. – Praveen Feb 26 '14 at 15:46
  • Why was 1900 added to the year? – gen_Eric Feb 26 '14 at 15:46
  • 1
    I just found out that it's an ISO-8601 date format string. – Euphe Feb 26 '14 at 15:46
  • @RocketHazmat, I get it from the server. Not much I can do. – Euphe Feb 26 '14 at 15:47
  • 4
    This might provide some insight to the 3913 deal, http://stackoverflow.com/questions/14803901/why-is-this-generating-3913-as-the-year – curtisk Feb 26 '14 at 15:47
  • The date is somehow a result of new Date(2013, 11, 11) being sent by the server as a property of a json object – Euphe Feb 26 '14 at 15:48
  • Looks like your server sends you crap. This date is the 10th of December in the year 3913. Standard date libraries should have no trouble parsing this. – mcv Feb 26 '14 at 15:49
  • @RocketHazmat, thanks! How do I parse it though? The year is understandable, but the 12-10T2 part doesn't look like 11/11 – Euphe Feb 26 '14 at 15:49
  • 1
    @Euphe: The `T` is a separator. `20:00:00+0000` is the time (with timezone offset), and `3913-12-10` is the date. JavaScript can parse this (`new Date(dateStr)`), but you're gonna need to offset the date correctly after. – gen_Eric Feb 26 '14 at 15:50
  • 1
    I think someone switched days and months; months often start at 0 in some internal representations, while days start at 1. Also, is the server in Perl? I believe Perl dates start at 1900. Server might be a pre-2000 one that expects only the last two digits. – mcv Feb 26 '14 at 15:50
  • @RocketHazmat, you should write an answer. It just worked. – Euphe Feb 26 '14 at 15:53
  • @Euphe: Maybe this has something to do with it? http://stackoverflow.com/q/98124 – gen_Eric Feb 26 '14 at 15:53
  • @Euphe: Do you have any control over the server creating this string? – gen_Eric Feb 26 '14 at 15:53
  • @RocketHazmat nope, no control. It works now though. – Euphe Feb 26 '14 at 15:58

3 Answers3

3

Do it just like this:

var d= new Date("3913-12-10T20:00:00+0000");
var newDate =  d.getDate() + '/' + d.getMonth() + '/' + d.getFullYear();

or any other date format function you know.

Cracker0dks
  • 2,422
  • 1
  • 24
  • 39
  • 1
    Except `d.getFullYear` will return you 3913 when the OP claims it should be 2013. – gen_Eric Feb 26 '14 at 15:55
  • when the input is 3913 you will get 3913.... this is normal Json dateTime format. – Cracker0dks Feb 26 '14 at 15:56
  • There's no such thing as "Json dateTime format". Also, if you read the question, the OP wants to "convert" this to 11/11/2013, since he claims it should be that. – gen_Eric Feb 26 '14 at 15:57
2

The server you are getting this data from is giving you bad data. You should report this to them so they can fix it. They are probably using deprecated APIs and/or building this string (incorrectly) by hand.

The string is valid ISO-8601, except the year is offset by +1900, the month by +1 and the day by -1.

So, one solution is to parse it as-is and then offset the date object. I, personally, love the moment.js library for dealing with dates in JavaScript.

var dateString = '3913-12-10T20:00:00+0000',
    dateObj = moment(dateString);

dateObj.subtract({'y': 1900, 'M': 1}).add('d', 1);

console.log(dateObj.format('MM/DD/YYYY')); // 11/11/2013

Or if you prefer using native Date objects:

var dateString = '3913-12-10T20:00:00+0000',
    dateObj = new Date(dateString);

dateObj.setFullYear(dateObj.getFullYear() - 1900);
dateObj.setMonth(dateObj.getMonth() - 1);
dateObj.setDate(dateObj.getDate() + 1);

console.log(dateObj.toDateString()); // Mon Nov 11 2013
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
-2
    function dateString(d, i) {

        var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

        var curr_date = d.getDate() + i;
        var sup = "";

        if (curr_date == 1 || curr_date == 21 || curr_date ==31){
           sup = "st";
        }
        else if (curr_date == 2 || curr_date == 22){
           sup = "nd";
        }
        else if (curr_date == 3 || curr_date == 23){
           sup = "rd";
        }
        else{
           sup = "th";
        }

        var curr_month = d.getMonth();
        var curr_year = d.getFullYear();

        var dateString = curr_date + sup + " " + m_names[curr_month] + " " + curr_year;

        return dateString;

    }
user3355603
  • 1,091
  • 2
  • 12
  • 19