Wednesday 14 March 2012

Convert unix date to datetime.

Definition: Convert Unixdate to DateTime stamp.

protected void Button1_Click(object sender, EventArgs e)
    {
        // UNIX timestamp
        double timestamp = 1309177737;

        // Step1 : Make System.DateTime equivalent to the UNIX Epoch.

        System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

        // Step 2: Add the number of seconds in UNIX timestamp to be converted.

        dateTime = dateTime.AddSeconds(timestamp);

        // The dateTime now contains the right date/time so to format the string,

        // use the standard formatting methods of the DateTime object.

        string printDate = dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString();

        // Print the date and time
        Response.Write(printDate);
    }

Output :
6/27/2011 12:28 PM

No comments:

Post a Comment