2012-04-08 36 views
0

我從其中包含使用下面轉換UNIX時間正常的日期和時間C#

此日期列代碼日期列的sqlite3的數據庫填充在我的程序的數據網格視圖存儲在Unix時間,我想顯示爲一個正常日期

有沒有一種方法可以做到這一點,因爲我正在讀數據庫到數據網格視圖?

SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder(); 
csb.DataSource = Path.Combine(connectionPath, "sms.db"); 

SQLiteConnection connection = new SQLiteConnection(csb.ConnectionString); 
connection.Open(); 

// SQL query to read the data fromt he database 
SQLiteCommand command = connection.CreateCommand(); 
//Read Everything 
string query = "SELECT * FROM message"; 

command.CommandText = query; 

SQLiteDataAdapter dataAdaptor = new SQLiteDataAdapter(command); 
DataSet dataset = new DataSet(); 
dataAdaptor.Fill(dataset, "Messages"); 
// Get the table from the data set 
DataTable datatable = dataset.Tables["Messages"]; 

dataGridSMS.DataSource = datatable; 
+1

http://www.sqlite.org/lang_datefunc.html – MarcinJuraszek 2012-04-08 16:39:49

回答

3
// This is an example of a UNIX timestamp for the date/time 
double timestamp = 1116641532; 

// First make a System.DateTime equivalent to the UNIX Epoch. 
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); 

// Add the number of seconds in UNIX timestamp to be converted. 
dateTime = dateTime.AddSeconds(timestamp).ToLocalTime(); 
0

使用DataGridView.CellFormatting Event

private void dataGridSMS_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (this.dataGridSMS.Columns[e.ColumnIndex].Name == "dateColumn") 
     { 
      if (e.Value != null) 
      { 
       try 
       { 
        // Formatting 
        double timestamp = Convert.ToDouble(e.Value); // if e.Value is string you must parse 
        System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); 
        dateTime = dateTime.AddSeconds(timestamp).ToLocalTime(); 
        e.Value = dateTime.ToString(); 
        e.FormattingApplied = true; 
       } 
       catch (Exception) 
       { 
        e.FormattingApplied = false; 
       } 
      } 
     } 
    }