时间到一天的时间 [英] time.Time Round to Day

查看:39
本文介绍了时间到一天的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间戳记,我想知道是否有办法在PST中将其舍入到一天的开始.例如,ts: 1305861602 对应于 2016-04-14,21:10:27 -0700 ,但我想将其舍入为映射到 2016-04-14 00:00:00 -0700 .我读了time.Time文档,但没有找到解决方法.

I have a timestamp coming in, I wonder if there's a way to round it down to the start of a day in PST. For example, ts: 1305861602 corresponds to 2016-04-14, 21:10:27 -0700, but I want to round it to a timestamp that maps to 2016-04-14 00:00:00 -0700. I read through the time.Time doc but didn't find a way to do it.

推荐答案

执行此操作的简单方法是使用前一个创建新的 Time ,并且仅分配年份的月份和日期.看起来像这样;

The simple way to do this is to create new Time using the previous one and only assigning the year month and day. It would look like this;

rounded := time.Date(toRound.Year(), toRound.Month(), toRound.Day(), 0, 0, 0, 0, toRound.Location())

这是一个播放示例; https://play.golang.org/p/jnFuZxruKm

这篇关于时间到一天的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆