// GetCurrentDateString 獲取當天的時間date func GetCurrentDateString() time.Time { // 獲取當前時間 currentTime := time.Now().Local() // 格式化日期爲字符串 dateString := currentTime.Format(time.DateTime) resultTime, _ := time.Parse(time.DateTime, dateString) return resultTime }
插入db之後發現時間+8了。
// GetCurrentDateString 獲取當天的時間date func GetCurrentDateString() time.Time { // 獲取當前時間 currentTime := time.Now().Local() // 格式化日期爲字符串 dateString := currentTime.Format(time.DateTime) resultTime, _ := time.ParseInLocation(time.DateTime, dateString, time.Local) return resultTime }
- 檢查鏈接字符串是否添加時區信息 例如 : parseTime=true&loc=Local
- 檢查服務器當前時區 是否正確: timedatectl | grep "Time zone"
- 檢查mysql 時區設置:SHOW VARIABLES LIKE '%time_zone%';
- 阿里雲AnalyticDB實例Datatime 類型最多精確到秒。使用TIMESTAMP可以存儲'2024-03-11 15:22:11.4688249' 。 使用起來和Mysql實例有差距