mysql中的datetime转时间戳
| |
| t := "2023-02-21T14:51:00+08:00" |
| ts, _ := time.ParseInLocation(time.RFC3339, t, time.Local) |
| loc, _ := time.LoadLocation("Local") |
| timeLayout := "2006-01-02 15:04:05" |
| theTime, _ := time.ParseInLocation(timeLayout, ts.Format("2006-01-02 15:04:05"), loc) |
| sr := theTime.Unix() |
| fmt.Println(sr) |
String转Time
常规格式
| t, _ := time.ParseInLocation("2006-01-02 15:04:05", "2022-12-15 14:35:31", time.Local) |
| fmt.Println(t) |
| ts, _ := time.ParseInLocation(time.RFC3339, "2022-12-15T14:35:31+08:00", time.Local) |
| fmt.Println(ts) |

time转string
| t := "2023-02-21T14:51:00+08:00" |
| ts, _ := time.ParseInLocation(time.RFC3339, t, time.Local) |
| fmt.Println(ts.Format("2006-01-02 15:04:05")) |
获取当前时间字符串
| date := time.Now().Format("2006-01-02 15:04:05") |
| date := time.Now().Format("2006-01-02 00:00:00") |
| fmt.Printf("当前时间:%v", date) |
获取时间戳
获取当前时间的时间戳
| now:= time.Now() |
| fmt.Println(now.Unix()) // 1565084298 秒 |
| fmt.Println(now.UnixNano()) // 1565084298178502600 纳秒 |
| fmt.Println(now.UnixNano() / 1e6) // 1565084298178 毫秒 |
获取指定时间的时间戳
| date := time.Now().Format("2006-01-02 00:00:00") |
| toBeCharge := date |
| timeLayout := "2006-01-02 15:04:05" |
| loc, _ := time.LoadLocation("Local") |
| theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) |
| sr := theTime.Unix() |
| fmt.Println(theTime) |
| fmt.Println(sr) |
时间戳转字符串
| package main |
| |
| import ( |
| "fmt" |
| "time" |
| ) |
| |
| func main() { |
| |
| timestamp := time.Now().Unix() |
| |
| |
| tm := time.Unix(timestamp, 0) |
| |
| |
| str := tm.Format("2006-01-02 15:04:05") |
| |
| |
| fmt.Println(str) |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通