我将如何在golang缩短完美的时间?

我将如何在golang缩短完美的时间?,go,time,Go,Time,我正在做一个cron工作,自动收取预订费。管理员在预订完成2小时后设置自动收费,然后我在当前时间2小时前完成所有预订并自动收费。但最终出现的问题是,current_date=10/10/2018 current_time=1:00AM和automatic_charge_hours=2(小时)意味着它将获得在当前时间和当前_date前2小时完成的所有预订。24小时制将获得前一日期23小时(2018年10月9日晚上11:00)的所有预订。但在我的情况下,它会变成零以下是我的情况:- package

我正在做一个cron工作,自动收取预订费。管理员在预订完成2小时后设置自动收费,然后我在当前时间2小时前完成所有预订并自动收费。但最终出现的问题是,
current_date=10/10/2018 current_time=1:00AM
automatic_charge_hours=2
(小时)意味着它将获得在当前时间和当前_date前2小时完成的所有预订。24小时制将获得前一日期23小时(2018年10月9日晚上11:00)的所有预订。但在我的情况下,它会变成零以下是我的情况:-

package main

import (
  "fmt"
  "time"
  "strings"
)

func main() {
timeZone, _ := time.LoadLocation("America/New_York")
currDate := time.Now().In(timeZone).Format("2006-01-02 00:00:00 -0000")
onlyDate := strings.Split(currDate, " ")
hours, _ := 1, 0
if hours-int(2) < 0 {
    hours = 0
} else {
    hours = hours - int(2)
}
fmt.Println(hours, onlyDate[0])
}
主程序包
进口(
“fmt”
“时间”
“字符串”
)
func main(){
时区:=time.LoadLocation(“美国/纽约”)
currDate:=time.Now().In(时区)格式(“2006-01-02 00:00:00-0000”)
onlyDate:=strings.Split(currDate,“”)
小时数:=1,0
如果小时数int(2)<0{
小时=0
}否则{
小时数=小时数-整数(2)
}
格式打印项次(小时,仅限日期[0])
}
运动场连接

我将如何改变它。有什么建议吗

if hours-int(2) < 0 {
    hours = 0
} else {
    hours = hours - int(2)
}

您只需稍微更改以下小时的条件:-

func main() {
   timeZone, _ := time.LoadLocation("America/New_York")
   currDate := time.Now().In(timeZone).Format("2006-01-02 00:00:00 -0000")
   onlyDate := strings.Split(currDate, " ")
   hours, _ := 1, 0
   hours = hours-int(2)
   fmt.Println(hours)
   if hours <= -1 {
           hours = 24 + hours
       currDate = time.Now().In(timeZone).AddDate(0, 0, -1).Format("2006-01-02 00:00:00 -0000")
       fmt.Println(currDate)
   } 
   fmt.Println(hours, onlyDate[0])
}
func main(){
时区:=time.LoadLocation(“美国/纽约”)
currDate:=time.Now().In(时区)格式(“2006-01-02 00:00:00-0000”)
onlyDate:=strings.Split(currDate,“”)
小时数:=1,0
小时=小时整数(2)
fmt.Println(小时)
如果小时考虑使用。
func main() {
   timeZone, _ := time.LoadLocation("America/New_York")
   currDate := time.Now().In(timeZone).Format("2006-01-02 00:00:00 -0000")
   onlyDate := strings.Split(currDate, " ")
   hours, _ := 1, 0
   hours = hours-int(2)
   fmt.Println(hours)
   if hours <= -1 {
           hours = 24 + hours
       currDate = time.Now().In(timeZone).AddDate(0, 0, -1).Format("2006-01-02 00:00:00 -0000")
       fmt.Println(currDate)
   } 
   fmt.Println(hours, onlyDate[0])
}