#region 將日期轉換成指定的格式 /// <summary> /// 將日期轉換成yyyy-mm-dd格式. /// </summary> /// <param name="NDate">日期</param> /// <returns>返回String對象</returns> public string Date_Format(string NDate) { string sm, sd; int y, m, d; try { y = Convert.ToDateTime(NDate).Year; m = Convert.ToDateTime(NDate).Month; d = Convert.ToDateTime(NDate).Day; } catch { return ""; } if (y == 1900) return ""; if (m < 10) sm = "0" + Convert.ToString(m); else sm = Convert.ToString(m); if (d < 10) sd = "0" + Convert.ToString(d); else sd = Convert.ToString(d); return Convert.ToString(y) + sm + sd; } #endregion #region 將日期轉換成指定的格式 /// <summary> /// 將日期轉換成yyyy-mm-dd格式. /// </summary> /// <param name="NDate">日期</param> /// <returns>返回String對象</returns> public string Date_Format_(string NDate) { string sm, sd; int y, m, d; try { y = Convert.ToDateTime(NDate).Year; m = Convert.ToDateTime(NDate).Month; d = Convert.ToDateTime(NDate).Day; } catch { return ""; } if (y == 1900) return ""; if (m < 10) sm = "0" + Convert.ToString(m); else sm = Convert.ToString(m); if (d < 10) sd = "0" + Convert.ToString(d); else sd = Convert.ToString(d); return Convert.ToString(y) + "-" + sm + "-" + sd; } #endregion #region 將時間轉換成指定的格式 /// <summary> /// 將時間轉換成hhmmss格式. /// </summary> /// <param name="NDate">日期</param> /// <returns>返回String對象</returns> public string Time_Format(string NDate) { string sh, sm, se; int hh, mm, ss; try { hh = Convert.ToDateTime(NDate).Hour; mm = Convert.ToDateTime(NDate).Minute; ss = Convert.ToDateTime(NDate).Second; } catch { return ""; } sh = Convert.ToString(hh); if (sh.Length < 2) sh = "0" + sh; sm = Convert.ToString(mm); if (sm.Length < 2) sm = "0" + sm; se = Convert.ToString(ss); if (se.Length < 2) se = "0" + se; return sh + sm + se; } #endregion #region 返回excel的路徑名 internal string getExcelFileName()//打開excel文件 { string name = ""; try { OpenFileDialog openCsv = new OpenFileDialog(); openCsv.Filter = "Excel 文件|*.xls;*.csv;*.et"; if (openCsv.ShowDialog() == DialogResult.OK) { name = openCsv.FileName; return name; } } catch (OleDbException ode) { MessageBox.Show(ode.ToString()); return null; } catch (Exception exc) { MessageBox.Show("未打開文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Console.WriteLine(exc.ToString()); } return null; } //打開文件 #endregion #region 返回時間格式到 yyyymmddhhmmss 20130930074600 internal string changeDateTimeFormat(string cardTime) { string dateFormat = Date_Format(cardTime); string timeFormat = Time_Format(cardTime); string dateAndTime = dateFormat + timeFormat; return dateAndTime; } #endregion