0

I tried to convert 1368463365 which is an int field in sql server database with the following code

public static DateTime JulianToDateTime(int julianDate)
{
    int RealJulian = julianDate + 1900000;
    int year = Convert.ToInt32(RealJulian.ToString().Substring(0, 4));
    int DoY = Convert.ToInt32(RealJulian.ToString().Substring(4));
    DateTime d = new DateTime(year, 1, 1);
    return d.AddDays(DoY - 1);
}
Pramesh
  • 1,194
  • 12
  • 16
  • 5
    Define "Julian Date" here. It looks like you are referring to the text representation of a mashed number since 1900 (which is not a [Julian Date](http://en.wikipedia.org/wiki/Julian_day)). – user2864740 Feb 18 '14 at 18:47
  • 5
    Show sample input and expected output, or describe more precisely your problem. – MarcinJuraszek Feb 18 '14 at 18:48
  • possible duplicate of [How to convert UNIX timestamp to DateTime and vice versa?](http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa) – L.B Feb 18 '14 at 18:59

5 Answers5

3

Your input is not a julian date. It's a timestamp. 1368463365 refers to Mon, 13 May 2013 16:42:45 GMT.

You can use following method to get DateTime from timestamp:

public static DateTime UnixTimeStampToDateTime( int unixTimeStamp )
{
    // Unix timestamp is seconds past epoch
    System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0);
    dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
    return dtDateTime;
}
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
3

It looks like you have a Unix timestamp there. The value 1368463365 would be equivalent with 13 May 2013 16:42:45 GMT.

A Unix timestamp is simply the number of seconds since midnight January 1st, 1970 UTC/GMT. So you can convert it to a regular DateTime like this:

public static DateTime UnixTimeToDateTime(long timestamp)
{
    var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    dateTime = dateTime.AddSeconds((double)timestamp);
    dateTime = dateTime.ToLocalTime();  // Change GMT time to your timezone
    return dateTime;
}

Adapted from this answer. Usage:

long timestamp = 1368463365;
Console.WriteLine(UnixTimeToDateTime(timestamp));

Result (on my Dutch computer, in UTC+2):

13-5-2013 18:42:45
Community
  • 1
  • 1
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
2

The number 1368463365 is Unix timestamp and it's number of seconds since 1/1/1970. In that case what you have to do is to just add this timestamp to DateTime representing the the date 1/1/1970 00:00:00.

Example code from another SO question:

public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )
{
    // Unix timestamp is seconds past epoch
    System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0);
    dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
    return dtDateTime;
}

Check this SO question for reference.

And BTW, there's a little place for mistake here, but if you want to check what date such timestamp represents, you can check it online with this converter.

Community
  • 1
  • 1
Konrad Gadzina
  • 3,407
  • 1
  • 18
  • 29
1

Insofar as I know, Julian Date can mean

  • The count of days since 1 January 4713 BCE at 12:00:00 pm (Noon) UTC in the Julian Calendar, which is 24 November 4714 BCE in the Gregorian calendar. Today 18 February 2014 is JD 2456706 (for at least part of the day.)

  • The ordinal day of the year (e.g. 31 December 2013 is 2013365; 31 December 2012 is 2012366.

None of these are 10 digits. For conversion to/from the former, see http://aa.usno.navy.mil/faq/docs/JD_Formula.php (your tax dollars at work...or at least my tax dollars at work).

Conversion to/from the ordinal date form should be pretty obvious:

string   julianDate       = "2014323" ; // the 323rd day of  2014
int      year             = int.Parse( julianDate.substring(0,4) ) ;
int      ordinalDayNumber = int.Parse( julianDate.substring(4,3) ) ;
DateTime dt               = new DateTime(year,1,1).AddDays( ordinalDayNumber - 1 ) ;
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

The unix time is a the number of seconds since midnight January 1, 1970 UTC.

DateTime UnixTimeToDateTime(int timestamp)
{
    return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp);
}
NickC
  • 385
  • 1
  • 10