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.

16
  • 3
    Doesn't look like 11/11/2013 to me. Commented Feb 26, 2014 at 15:45
  • 1
    I just found out that it's an ISO-8601 date format string. Commented Feb 26, 2014 at 15:46
  • 4
    This might provide some insight to the 3913 deal, stackoverflow.com/questions/14803901/… Commented Feb 26, 2014 at 15:47
  • 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. Commented Feb 26, 2014 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. Commented Feb 26, 2014 at 15:50

3 Answers 3

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.

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

3 Comments

Except d.getFullYear will return you 3913 when the OP claims it should be 2013.
when the input is 3913 you will get 3913.... this is normal Json dateTime format.
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.
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

Comments

-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;

    }

1 Comment

I'm not aware of any getDate() function on strings.

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.