Golang中如何获取日期而不包含时间部分

Golang中如何获取日期而不包含时间部分 我知道可以使用类似下面的代码来按照我想要的格式格式化时间:

// Golang程序以各种格式获取当前日期和时间
package main

import (
    "fmt"
    "time"
)

func main() {

    // 使用 time.Now() 函数获取当前时间
    currentTime := time.Now()

    // 以字符串格式获取时间
    fmt.Println("以字符串形式显示当前时间:", currentTime.String())

    fmt.Println("YYYY.MM.DD:", currentTime.Format("2017.09.07 17:06:06"))

    fmt.Println("YYYY#MM#DD {特殊字符}:", currentTime.Format("2017#09#07"))

    fmt.Println("MM-DD-YYYY:", currentTime.Format("09-07-2017"))

    fmt.Println("YYYY-MM-DD:", currentTime.Format("2017-09-07"))

    fmt.Println("YYYY-MM-DD hh:mm:ss:", currentTime.Format("2017-09-07 17:06:06"))

    fmt.Println("带微秒的时间:", currentTime.Format("2017-09-07 17:06:04.000000"))

    fmt.Println("带纳秒的时间:", currentTime.Format("2017-09-07 17:06:04.000000000"))

    fmt.Println("短数字宽度:", currentTime.Format("2017-02-07"))

    fmt.Println("短年份:", currentTime.Format("06-Feb-07"))

    fmt.Println("长星期:", currentTime.Format("2017-09-07 17:06:06 Wednesday"))

    fmt.Println("短星期:", currentTime.Format("2017-09-07 Wed"))

    fmt.Println("短日期:", currentTime.Format("Wed 2017-09-2"))

    fmt.Println("长月份:", currentTime.Format("2017-March-07"))

    fmt.Println("短月份:", currentTime.Format("2017-Feb-07"))

    fmt.Println("短时分秒:", currentTime.Format("2017-09-07 2:3:5 PM"))

    fmt.Println("短时分秒:", currentTime.Format("2017-09-07 2:3:5 pm"))

    fmt.Println("短时分秒:", currentTime.Format("2017-09-07 2:3:5"))

}

但这返回的是一个 string 而不是一个 date,所以如果我使用:

d := currentTime.Format("06-Feb-07")

那么 d 是一个字符串,但我需要让 d 成为一个 date 类型?


更多关于Golang中如何获取日期而不包含时间部分的实战教程也可以访问 https://www.itying.com/category-94-b0.html

4 回复

Date 是一个结构体。当你格式化它时,会得到字符串。也许,你需要的是指定日期的开始时刻?你打算在哪里使用它?

更多关于Golang中如何获取日期而不包含时间部分的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


仅包含日期部分(不含时间):

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	fmt.Println(now.Round(0))
	y, m, d := now.Date()
	fmt.Println(y, m, d)
	fmt.Println(y, int(m), d)
}

https://play.golang.org/p/cYO8KV4jLoQ

2009-11-10 23:00:00 +0000 UTC
2009 November 10
2009 11 10

那么 d 是一个字符串,但我需要将 d 作为一个 date 类型?

我不知道这是否对你有帮助,但我之前问过同样的问题。

我正在寻找用 SQL 值填充 Go 模板的方法。到目前为止,我有 3 种解决方案,各有优缺点。然而,日期的显示方式有些奇怪。

在 PGAdmin 中,日期显示正确:2020-08-26 使用 sql 标准库,日期显示为:2020-08-26 00:00:00 +0000 +0000 使用 sqlx,日期显示为:2020-08-26T00:00:00Z 使用 json,日期显示正确:2020-08-26

或者,Go 结构体中没有日期格式吗?

  1. Sql 标准库 标准 sql 库是惯用的,但…

在Go语言中,time.Time类型总是包含日期和时间部分。要获取只包含日期的time.Time值,可以使用Truncate方法将时间部分归零,或者使用Date函数创建新的时间值。

以下是几种获取纯日期(时间部分为00:00:00)的方法:

方法1:使用Truncate方法

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    
    // 将时间截断到天级别,时间部分变为00:00:00
    dateOnly := now.Truncate(24 * time.Hour)
    
    fmt.Println("原始时间:", now)
    fmt.Println("仅日期部分:", dateOnly)
    fmt.Println("格式化显示:", dateOnly.Format("2006-01-02"))
}

方法2:使用Date函数创建新时间

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    
    // 提取年、月、日,创建新的时间(时间部分为00:00:00)
    year, month, day := now.Date()
    dateOnly := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
    
    fmt.Println("原始时间:", now)
    fmt.Println("仅日期部分:", dateOnly)
    fmt.Println("格式化显示:", dateOnly.Format("2006-01-02"))
}

方法3:使用自定义函数

package main

import (
    "fmt"
    "time"
)

// 获取指定时间的日期部分(时间归零)
func DateOnly(t time.Time) time.Time {
    return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}

func main() {
    now := time.Now()
    dateOnly := DateOnly(now)
    
    fmt.Println("原始时间:", now)
    fmt.Println("仅日期部分:", dateOnly)
    fmt.Println("ISO格式:", dateOnly.Format("2006-01-02"))
    fmt.Println("自定义格式:", dateOnly.Format("02-Jan-2006"))
}

方法4:使用Format和Parse组合(不推荐)

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    
    // 格式化为字符串
    dateStr := now.Format("2006-01-02")
    
    // 解析回time.Time(时间部分为00:00:00)
    dateOnly, err := time.Parse("2006-01-02", dateStr)
    if err != nil {
        panic(err)
    }
    
    fmt.Println("日期字符串:", dateStr)
    fmt.Println("解析后的时间:", dateOnly)
}

实际应用示例

package main

import (
    "fmt"
    "time"
)

func main() {
    // 获取今天的日期(时间部分为00:00:00)
    today := time.Now().Truncate(24 * time.Hour)
    
    // 获取明天的日期
    tomorrow := today.Add(24 * time.Hour)
    
    // 获取昨天的日期
    yesterday := today.Add(-24 * time.Hour)
    
    fmt.Println("今天:", today.Format("2006-01-02"))
    fmt.Println("明天:", tomorrow.Format("2006-01-02"))
    fmt.Println("昨天:", yesterday.Format("2006-01-02"))
    
    // 比较日期
    someTime := time.Date(2024, 1, 15, 14, 30, 0, 0, time.UTC)
    someDate := someTime.Truncate(24 * time.Hour)
    
    if today.Equal(someDate) {
        fmt.Println("今天就是2024-01-15")
    } else if today.Before(someDate) {
        fmt.Println("今天在2024-01-15之前")
    } else {
        fmt.Println("今天在2024-01-15之后")
    }
}

关键点:

  1. Go中没有独立的date类型,只有time.Time
  2. 通过将时间部分设置为00:00:00来模拟纯日期
  3. Truncate(24*time.Hour)是最简洁的方法
  4. 格式化输出时使用Format("2006-01-02")获取日期字符串

这些方法返回的都是time.Time类型,但时间部分被归零,可以当作纯日期使用。

回到顶部