Golang为日期减去特定的时间。

huangapple go评论102阅读模式
英文:

Golang deduct a specific time for a date

问题

我有一个类似于2014-10-22 00:00的日期,我想要减去330分钟,以下是我使用的代码,但是它不起作用,我尝试使用负的持续时间来减少时间,然而时间并没有减少。原本的"开始日期"是2014-12-29 00:00,在减去之后,仍然是2014-12-29 00:00,这意味着减法没有起作用。

layoutStart := "2006-01-02 15:04"
layoutEnd := "2006-01-02 15:04"

var st time.Time
var et time.Time

var err error
var err2 error

var neg_india_offset = time.Duration(-330) * time.Minute

if st, err = time.Parse(layoutStart, startDate); err == nil {
    log.Printf("开始时间解析成功: %v", st)
    st = st.Add(neg_india_offset)
    log.Printf("开始时间减去330分钟: %v", st)
} else {
    log.Printf("解析开始时间失败: %v", err)
}

if et, err2 = time.Parse(layoutEnd, endDate); err2 == nil {
    log.Printf("结束时间解析成功: %v", et)
    et = et.Add(neg_india_offset)
    log.Printf("结束时间减去330分钟: %v", et)
} else {
    log.Printf("解析结束时间失败: %v", err2)
}

希望这可以帮助你解决问题。

英文:

I have a date in golang like 2014-10-22 00:00 , and I want to deduct in 330 minutes , the following is the code I used, however, it does not works, I tried to reduce the time with negative duration, however , the time is not reduced. Originally, "Start date" is 2014-12-29 00::00, after deduction, it is still 2014-12-29 00:00, that means the deduction is not work.

    layoutStart := "2006-01-02 15:04"
	layoutEnd := "2006-01-02 15:04"

	var st time.Time
	var et time.Time

	var err error
	var err2 error

	var neg_india_offset = time.Duration(-330) * time.Minute
	//var day_offset time.Duration = 1439 * time.Minute

	if st, err = time.Parse(layoutStart, startDate); err == nil {
		log.Printf("Start Time decoded: %v", st)
		st.Add(neg_india_offset)
		log.Printf("Start Time deduct: %v", st)
	} else {
		log.Printf("Failed to decode start time: %v", err)
	}

	if et, err2 = time.Parse(layoutEnd, endDate); err2 == nil {
		log.Printf("End Time decoded: %v", et)
		st.Add(neg_india_offset)
		log.Printf("End Time added day: %v", et)
	} else {
		log.Printf("Failed to decode end time: %v", err2)
	}

答案1

得分: 1

函数time.Time.Add()不会覆盖其参数,而是返回新的时间。将以下行代码进行修改:

st.Add(neg_india_offset)

修改为:

st = st.Add(neg_india_offset)

如果你想要获取一个特定时区的日期,请查看time.ParseInLocation()函数。它可以解析指定时区的日期,使用方法如下:

loc, err := time.LoadLocation("India/Delhi")
// ...

st, err = time.ParseInLocation(layoutStart, startDate, loc)
英文:

The function time.Time.Add() does not overwrite its argument. It returns the new time. Change the line

st.Add(neg_india_offset)

to

st = st.Add(neg_india_offset)

If you want to get a date with a specific time-zone, have a look at the time.ParseInLocation() function. It can parse a date for a specified time zone; use it like this:

loc, err := time.LoadLocation("India/Delhi")
// ...

st, err = time.ParseInLocation(layoutStart, startDate, loc)

huangapple
  • 本文由 发表于 2014年12月30日 05:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/27696172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定