views:

75

answers:

3

I have the int number 2455449 and I know that represents the date 09/09/2010. How can I define the date format wihch is used? I need to generate a new date in this format. It will be used for http requests. I suppose that is Julian but I'm not sure. I tried to convert this number to the date but it didn't return the right date of 09/09/2010. Probably I used a wrong SimpleDateFormat("mm/dd/yy") or Calendar.XXXX (e.g.Calendar.DAY_OF_YEAR)

+2  A: 
var now = new Date();

now.format("m/dd/yy");
// Returns, e.g., 6/09/07

// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM

// You can use one of several named masks
now.format("isoDateTime");
// 2007-06-09T17:46:21

// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
now.format("hammerTime");
// 17:46! Can't touch this!

http://blog.stevenlevithan.com/archives/date-time-format

Robert
+1  A: 

looks like an equation with unknown function.

f(d,m,y)=D;
Where d the day, m month,y year and D is int date. And without loss of generality we can assume that this mapping should be one to one i.e. every valid (d,m,y) combination should map to a unique positive integer (>=0) and every positive integer must represent a valid and unique (d,m,y) tuple.
So the most obvious choice of function f (based on the property of dates) is number of days elapsed since the first day, which satisfies our conditions. so now we have boundary condition.
f(d1,m1,y1)=0;
f(9,9,2010)= 2455449;
where d1,m1,y1 represents the reference date like epoch in unix timestamp. Using the obvious function (see above), (d1,m1,y1) comes out to be (10 5 -4713). So the DatFormat used is number of days elapsed since 10th June 4713 B.C. Approximately.

bhups
Formal win ;-) Would be nice to validate your results with another sampling point - user341203, could you enter another date and tell us the result?
Steffen Müller
I think it should be January 1, 4713 B.C, the epoch of the Julian day: http://en.wikipedia.org/wiki/Julian_day#Converting_Gregorian_calendar_date_to_Julian_Day_Number
some
A: 

It is a Julian day number, and it counts the number of days since January 1, 4713 BC Greenwich noon in the Julian proleptic calendar.

To convert from a JD to a unix time stamp:

unix_time_stamp = ( JD -2440587.5) * 86400

To convert from unix time stamp to JD:

JD = (unix_time_stamp / 86400) + 2440587.5

Note that JD is counted from the noon, not midnight. That's why it's .5 at the end of the addition.

Update If you want to use it in javascript (that uses milliseconds since the epoch)

function dateFromJulianDay(julian_day) {
  return new Date( (julian_day - 2440587.5) * 86400000);
}

function dateToJulianDay(date) {
  // date should be a javascript Date object
  // or a variable with milliseconds since the unix epoch 1 jan 1970
  return ( date / 86400000) + 2440587.5;
}

console.log(dateFromJulianDay(2455449));

console.log(dateToJulianDay(new Date(2010,9-1,9)));

Remember that the month in the Date constructor is 0-11, whats why I do -1 above.

some
Many thanks! I have found a better solution for me - http://www.rgagnon.com/javadetails/java-0506.html
@user341203: Is this question wrongly marked as javascript? Are you using dates from different centuries? The code that you link to looks very complicated compared to one-lines like `Calendar.setTimeInMillis((julian_day - 2440587.5) * 86400000)` and `(Calendar.getTimeInMillis() / 86400000) + 2440587.5` (I haven't tested this java code, but you get the idea). I'm just curious! :)
some