1. //获取本地location
  2. toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
  3. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  4. loc, _ := time.LoadLocation("Local") //重要:获取时区
  5. theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
  6. sr := theTime.Unix() //转化为时间戳 类型是int64
  7. fmt.Println(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST
  8. fmt.Println(sr) //打印输出时间戳 1420041600
  9. //时间戳转日期
  10. dataTimeStr := time.Unix(sr, 0).Format(timeLayout) //设置时间戳 使用模板格式化为日期字符串
  11. fmt.Println(dataTimeStr)

参考: http://blog.csdn.net/u012210379/article/details/44747311

分类: web

标签:   golang