将四个字符的日期字符串转换为日期字符串。

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

Convert four character date string to a date string

问题

我正在接收到一个类似于"1226"的四位字符串作为到期日期。我需要从这个数据中创建一个日期字符串。

到目前为止,我可以获取到月份和年份。我尝试使用文档中提到的time.Parse方法,但我无法弄清楚。

func main() {

    dateData := "1226"
    month := dateData[:2]
    year := dateData[2:]
    t, _ := time.Parse("2006-01-02", fmt.Sprintf("20%s-%s-00", year, month))
    fmt.Println(t)
    // 期望的输出是"2026-12-01",如果我们能在此基础上再加一个月就更好了,那么它将变成"2027-01-01"

}
英文:

I am receiving the expiry date as a four-letter string like this, "1226"
I need to create a date string from that data.

so far I can get the month and year. tried using time.Parse method was mentioned in the docs but I couldn't figure it out.

func main() {

	dateData := "1226"
	month := dateData[:2]
	year := dateData[2:]
	t, _ := time.Parse("2006-01-02", fmt.Sprintf("20%s-%s-00", year, month))
	fmt.Println(t)
	// expected output is "2026-12-01", and also it's better if we could add one month to this one. then it will become "2027-01-01"

}

答案1

得分: 4

我收到的到期日期是一个四位数的字符串,像这样:"1226"

预期的输出是"2026-12-01"

package main

import (
	"fmt"
	"time"
)

func main() {
	date := "1226"
	if len(date) == 4 {
		date = date[0:2] + "20" + date[2:4]
	}
	t, err := time.Parse("012006", date)
	fmt.Println(t, err)
}

输出结果为:2026-12-01 00:00:00 +0000 UTC

如果我们能够在这个日期上加一个月,那就更好了,它将变成"2027-01-01"

package main

import (
	"fmt"
	"time"
)

func main() {
	date := "1226"
	if len(date) == 4 {
		date = date[0:2] + "20" + date[2:4]
	}
	t, err := time.Parse("012006", date)
	if err == nil {
		t = t.AddDate(0, 1, 0)
	}
	fmt.Println(t, err)
}

输出结果为:2027-01-01 00:00:00 +0000 UTC

英文:

> I am receiving the expiry date as a four-letter string like this, "1226"


> expected output is "2026-12-01

package main

import (
	"fmt"
	"time"
)

func main() {
	date := "1226"
	if len(date) == 4 {
		date = date[0:2] + "20" + date[2:4]
	}
	t, err := time.Parse("012006", date)
	fmt.Println(t, err)
}

https://go.dev/play/p/fwfBux1AZQO

2026-12-01 00:00:00 +0000 UTC <nil>

> it's better if we could add one month to this one. then it will become "2027-01-01"

package main

import (
	"fmt"
	"time"
)

func main() {
	date := "1226"
	if len(date) == 4 {
		date = date[0:2] + "20" + date[2:4]
	}
	t, err := time.Parse("012006", date)
	if err == nil {
		t = t.AddDate(0, 1, 0)
	}
	fmt.Println(t, err)
}

https://go.dev/play/p/xCAyxPdRjib

2027-01-01 00:00:00 +0000 UTC <nil>

答案2

得分: -1

这个对你有用吗?

https://go.dev/play/p/hrwVa_CvGYL


func DateStringFromTime(t time.Time) string {
	return (strings.Split(fmt.Sprint(t), " "))[0]
}

func TimeFromMMYY(dateData string) (time.Time, error) {

	t := time.Time{}

	if len(dateData) != 4 {
		return t, errors.New("bad date string")
	}

	month := dateData[:2]
	year := dateData[2:]
	m, err := strconv.Atoi(month)
	if err != nil {
		return t, err
	}

	y, err := strconv.Atoi(year)
	if err != nil {
		return t, err
	}

	loc := time.UTC // or time.Local
	t = time.Date(2000+y, time.Month(m), 1, 0, 0, 0, 0, loc)

	return t, nil
}
func main() {

	dateData := "1226"

	t, _ := TimeFromMMYY(dateData)

	fmt.Println(DateStringFromTime(t)) // prints 2026-12-01

	// add a month
	t = t.AddDate(0, 1, 0)
	fmt.Println(DateStringFromTime(t)) // prints 2027-01-01
}
英文:

Does this work for you?

https://go.dev/play/p/hrwVa_CvGYL


func DateStringFromTime(t time.Time) string {
	return (strings.Split(fmt.Sprint(t), " "))[0]
}

func TimeFromMMYY(dateData string) (time.Time, error) {

	t := time.Time{}

	if len(dateData) != 4 {
		return t, errors.New("bad date string")
	}

	month := dateData[:2]
	year := dateData[2:]
	m, err := strconv.Atoi(month)
	if err != nil {
		return t, err
	}

	y, err := strconv.Atoi(year)
	if err != nil {
		return t, err
	}

	loc := time.UTC // or time.Local
	t = time.Date(2000+y, time.Month(m), 1, 0, 0, 0, 0, loc)

	return t, nil
}
func main() {

	dateData := "1226"

	t, _ := TimeFromMMYY(dateData)

	fmt.Println(DateStringFromTime(t)) // prints 2026-12-01

	// add a month
	t = t.AddDate(0, 1, 0)
	fmt.Println(DateStringFromTime(t)) // prints 2027-01-01
}

huangapple
  • 本文由 发表于 2022年4月24日 11:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/71985303.html
匿名

发表评论

匿名网友

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

确定