golang 时间戳处理及转换


/*
 * 描述: 字符转换为时间戳
 *
 * 	strTime : 格式为 "2018-01-10"
 *
 ***************************************************************************/
func StringToTime(strTime string) int64 {
	//获取本地location
	strTem := "2006-01-02"
	timeLocal, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation(strTem, strTime, timeLocal)
	return theTime.Unix()
}

/*
 * 描述: 整数转换为时间戳
 *
 ***************************************************************************/
func IntToTime(nTimer int64) time.Time {
	return time.Unix(nTimer, 0)
}

/*
 * 描述: 字符转换为时间戳
 *
 * 	strTime : 格式为 "2018-01-10 03:04:05"
 *
 ***************************************************************************/
func StringToTimeEx(strTime string) int64 {
	//获取本地location
	strTem := "2006-01-02 15:04:05"
	timeLocal, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation(strTem, strTime, timeLocal)
	return theTime.Unix()
}


/*
 * 描述: 时间戳转格式化为字符串
 *
 ***************************************************************************/
func TimeToString(nTimer int64) string {
	tm := time.Unix(nTimer, 0)
	//strTime := tm.Format("2006-01-02 03:04:05 PM")
	return tm.Format("20060102")
}

/*
 * 描述: 时间戳转格式化为字符串
 *
 ***************************************************************************/
func TimeToStringEx(nTimer int64) string {
	tm := time.Unix(nTimer, 0)
	return tm.Format("2006-01-02 03:04:05 PM")
}

/*
 * 描述: 查看 输入时间戳 是否是今天
 *
 * 	checkTime : 查看的时间戳
 *
 ***************************************************************************/
func IsToday(checkTime int64) bool {
	nowTime := time.Now().Unix()
	nowTime -= (nowTime + 28800) % 86400
	if checkTime > nowTime {
		return true
	}
	return false
}

/*
 * 描述: 获取输入 时间戳 的零点时间戳
 *
 * 	checkTime : 所求的时间戳
 *
 ***************************************************************************/
func GetZero(checkTime int64) int64 {
	checkTime -= (checkTime + 28800) % 86400
	return checkTime
}

/*
 * 描述: 整点,向下取整
 *
 * 	checkTime : 所求的时间戳
 *
 ***************************************************************************/
func GetHourZero(checkTime uint32) uint32 {
	return checkTime - (checkTime % 3600)
}


/*
 * 描述: 求所当前月1号到下个月1号,两个时间戳
 *
 ***************************************************************************/
func Interval()( int64, int64 ){
	year  := time.Now().Year()
	month := time.Now().Month()
	stime := time.Date( year, month, 1, 0, 0, 0, 0, time.Local).Unix()
	etime := time.Date( year, ( month + 1 ), 1, 0, 0, 0, 0, time.Local).Unix()
	return stime, etime
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值