5

so, i need format JSON date from this format

"9/30/2010 12:00:00 AM", it is MM/DD/YYYY HH:MM:SS to format like this : DD/MM/YYYY, so i dont need info about hours, min and sec, and i need replace months and days from json, i tried some different ways but it always failed

i need do this using jQuery

also i didnt find any answer to formating this date type, all i found was formating date like this :/Date(1224043200000)/

so anyone have idea?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
hullfan
  • 75
  • 1
  • 1
  • 8

4 Answers4

4

you can create a Date Object from a string like so:

var myDate = new Date(dateString);

then you can manipulate it anyway you want, one way to get your desired output is:

var output = myDate.getDate() + "\\" +  (myDate.getMonth()+1) + "\\" + myDate.getFullYear();

you can find more at this elated.com article "working with dates"

JKirchartz
  • 17,612
  • 7
  • 60
  • 88
0

Unfortunately your "from" dateformat is not the one which is implementation-independent in JavaScript. And all the other formats depends on the implementation, which means even if this format would be understood by most of the implementation I/you can't be sure for example how the DD and MM order would be parsed (I am almost sure it would be local regional settings dependent). So I would recommend to use a 3rd party (or your hand written) date parser to get a Date object out of your input string. One such parser you can find here: http://www.mattkruse.com/javascript/date/

Because your question is not 100% clear for me, it's possible that you have your date in the format of /Date(number)/ which suggests that you are calling an ASP.Net service from your jQuery code. In this case during the JSON parse you can convert it to a Date object:

data = JSON.parse(data, function (key, value) {
    // parsing MS serialized DateTime strings
    if (key == '[NAME_OF_DATE_PROPERTY_IN_THE_JSON_STRING]') {
        return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
        // maybe new Date(parseInt(value.substr(6))) also works and it's simpler
    }
    return value;
});
peterfoldi
  • 7,451
  • 5
  • 21
  • 19
-1

The code below solved my problem:

var date = new Date(parseInt(d.data[i].dtOrderDate.replace("/Date(", "").replace(")/", ""), 10));
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
Antiga
  • 2,264
  • 1
  • 20
  • 28
-2

Try something like this :

var date = new Date(parseInt(jsonDate.substr(6))); 

where jsonDate is variable that stores your date

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Dana Addler
  • 973
  • 8
  • 4
  • 2
    You should add the base to `parseInt`: `parseInt(x, 10)`. – gen_Eric Feb 20 '12 at 15:26
  • 3
    I think you'll notice the OP wanted to obtain a date from a date string in `MM/DD/YYYY HH:MM:SS` format, not `/Date(ticks)/` format. – lsuarez Feb 20 '12 at 15:26
  • substr is not good for me, cos substr just cut the rest of string, so it cuts all after 7th character, but sometimes number of characters are not the same all the time, cos date format can be something like this: 1/2/2012 (8 characters) and sometimes it could be 31/10/2011 (10 characters) – hullfan Feb 20 '12 at 17:42