代码拉取完成,页面将自动刷新
// Copyright 2017 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author: polaris polaris@studygolang.com
package util
import "time"
import "fmt"
// MonthDayNum t 所在时间的月份总天数
func MonthDayNum(t time.Time) int {
isLeapYear := isLeap(t.Year())
month := t.Month()
switch month {
case time.January, time.March, time.May, time.July, time.August, time.October, time.December:
return 31
case time.February:
if isLeapYear {
return 29
}
return 28
default:
return 30
}
}
func TimeAgo(t time.Time) string {
now := time.Now()
diff := now.Sub(t)
hours := diff.Hours()
if hours < 1.0 {
return fmt.Sprintf("约 %.0f 分钟前", diff.Minutes())
}
if hours < 24.0 {
return fmt.Sprintf("约 %.0f 小时前", hours)
}
if hours < 72.0 {
return fmt.Sprintf("约 %.0f 天前", hours/24.0)
}
// 同一年,不用年份
if now.Year() == t.Year() {
return t.Format("01-02 15:04")
}
return t.Format("2006-01-02")
}
// 是否闰年
func isLeap(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。