[英]How can i add two date forms with time.Parse?
我正在嘗試添加兩種不同的日期形式,以便在測試過程中更加靈活,但出現以下錯誤:
我嘗試解析日期時收到的錯誤 --> 2021/10/21 12:15:35 將時間“1982/01/08”解析為“2006-01-02”:無法解析“/01/08”為“——”
BD.go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
// Users struct which contains
// an array of users
type Users struct {
Users []User `json:"users"`
}
// User struct which contains a name
// a type and a list of social links
type User struct {
Firstname string `json:"fname"`
Secondname string `json:"lname"`
Date string `json:"date"`
}
func Birthday() {
// Open our jsonFile
jsonFile, err := os.Open("users.json")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// we initialize our Users array
var users Users
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &users)
// we iterate through every user within our users array and
// print out the user Type, their name, and their facebook url
// as just an example
// write a function to list out the people whose birthday is today.
// var yr int
for i := 0; i < len(users.Users); i++ {
date, err := time.Parse("2006/01/02", users.Users[i].Date)
date1, _ := time.Parse("2006-01-02", users.Users[i].Date)
if err != nil {
fmt.Println(date)
} else if err != nil {
fmt.Println(date1)
} else {
log.Fatal(err)
}
// check if the date is a leap year, ex: 29 is not a leap year but 28th is !
if date.Day()%400 == 0 || (date.Day()%4 == 0 && date.Day()%100 != 0) {
fmt.Println("User First Name: " + users.Users[i].Firstname)
fmt.Println("User Second Name: " + users.Users[i].Secondname)
fmt.Println("User Date: " + users.Users[i].Date)
fmt.Println(users.Users[i].Date, " is a Leap Year ✨✨✨ ")
} else if date1.Day()%400 == 0 || (date1.Day()%4 == 0 && date1.Day()%100 != 0) {
fmt.Println("User First Name: " + users.Users[i].Firstname)
fmt.Println("User Second Name: " + users.Users[i].Secondname)
fmt.Println("User Date: " + users.Users[i].Date)
fmt.Println(users.Users[i].Date, " is a Leap Year ✨✨✨ ")
} else {
fmt.Println("User First Name: " + users.Users[i].Firstname)
fmt.Println("User Second Name: " + users.Users[i].Secondname)
fmt.Println("User Date: " + users.Users[i].Date)
fmt.Println(users.Users[i].Date, " is Not a Leap Year 💥💥💥 ")
}
}
}
func main() {
Birthday()
}
.JSON 文件
{
"users": [
{
"Fname": "Johnny",
"Lname":"mane",
"date":"1982/01/08"
},
{
"Fname": "Wayne",
"Lname":"Bruce",
"date":"1965/01/30"
},
{
"Fname": "Gaga",
"Lname":"Lady",
"date":"1986/03/28"
},
{
"Fname": "radio",
"Lname":"head",
"date":"1988/02/29"
},
{
"Fname": "Mario",
"Lname":"torres",
"date":"1996/09/28"
},
{
"Fname": "robert",
"Lname":"Alex",
"date":"1991/12/01"
},
{
"Fname": "Julia",
"Lname":"sevak",
"date":"1991-03-07" -->
},
{
"Fname": "feb",
"Lname":"robert",
"date":"1995-01-31". --->
}
]
}
您的錯誤處理似乎是錯誤的。 您需要在第一次嘗試解析日期時檢查錯誤。 如果失敗,請嘗試使用不同的布局進行解析:
date, err := time.Parse("2006/01/02", users.Users[i].Date)
if err != nil {
// first attempt failed, so try again...
date, err = time.Parse("2006-01-02", users.Users[i].Date)
if err != nil {
// both attempts failed
}
}
// all ok
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.