user1058272
user1058272

Reputation: 113

Conversion of double to datetime always fails

I open the History.plist file of Safari browser using a notepad and I find the encoded datetime of an accessed URL is 9 digits+DOT+one digit. I can't figure out how to interpret it to a suited datetime format, I wish to change it into the current date.

Code

DateTime dt=DateTime.FromOADate(348020617.0);

Upvotes: 1

Views: 4266

Answers (3)

Aditya
Aditya

Reputation: 44

You can make a method like below:
First store date in string after that use this method(it is for date length eight) but it can be increase or decrease.

private DateTime ConvertToDate(string date)
                    {
                        if (date.Length !=8)
                        {
                            return ConvertToDate("");
                        }
            int iYear; int.TryParse(date.Substring(0, 4), out iYear);
            int iMonth; int.TryParse(date.Substring(4, 2), out iMonth);
            int iDay; int.TryParse(date.Substring(6, 2), out iDay);
            return new DateTime(iYear, iMonth, iDay);
        }

Upvotes: 0

Chris Shain
Chris Shain

Reputation: 51339

If this is a UNIX timestamp, then you can convert using this function (borrowed from http://codeclimber.net.nz/archive/2007/07/10/convert-a-unix-timestamp-to-a-.net-datetime.aspx)

static DateTime ConvertFromUnixTimestamp(double timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(timestamp);
}

For what it's worth, that timestamp you mentioned (348020617.0) converts to 01/10/81 @ 6:23:37pm EST

Upvotes: 2

KV Prajapati
KV Prajapati

Reputation: 94645

From MSDN article :

DateTime.FromOADate(d); Where d must be a value between negative 657435.0 through positive 2958466.0.

Upvotes: 3

Related Questions