C 中DateTime型別資料格式化

2021-08-21 01:28:26 字數 471 閱讀 1541

今天做專案時遇到了這樣乙個問題,我需要把乙個string型別的資料轉化為datetime型別的資料,但轉換後的資料格式卻不是我們想要的標準格式(yyyy-mm-dd hh:mm:ss格式),轉換後的datetime資料的格式是與當前系統設定的時間格式是一樣的,

十分驚恐!!!!怎麼辦!!!程式肯定不能依賴於系統啊,,,,心裡歪歪   穩住,我們能贏!!!

1、string nowtime = nowtimes.tostring("yyyy-mm-dd hh:mm:ss");

這種方法用法比較簡單,但每個時間變數都要這樣寫有些麻煩,so~~

2、thread.currentthread.currentculture = new cultureinfo("zh-cn", true) };

只要在程式裡寫這句**,它會自動把你的所有時間格式化為自己定義的樣式,而不用手動的乙個乙個去格式化

注:datetimeformat的這三個缺一不可

C 中的DateTime型別

console.writeline datetime datetime.now.tostring yyyymmddhhmmssffff 引數format格式詳細用法 格式字元 關聯屬性 說明 d shortdatepattern d longdatepattern f 完整日期和時間 長日期和短時間...

C 中的DateTime型別加減

datetime date1 datetime.now console.writeline date1.tostring datetime d2 date1.addyears 1 注意這裡必須要再賦值給乙個新的變數。console.write d2.tostring 顯示結果 2010 5 23 1...

C 中DateTime資料型別和16進製制的資料轉化

思路可以把datetime和乙個時間戳相減得到timespan型別,然後轉化成秒數得到byte陣列 由陣列轉化成tostring x2 反之則反,下面上 using system using system.collections.generic using system.text namespace...