我正在嘗試將 json 檔案中的日期轉換為我的 .go 腳本中的正常日期,我想檢查它們是否為閏年,我已經完成了這部分,但我想將其轉換為日期,但是我不知道如何,我嘗試使用strconv.Itoa(users.Users[i].Date)但它沒有用!
將不勝感激任何反饋或幫助,謝謝!
我嘗試同時添加兩種型別的日期表單時收到的錯誤 --> 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 main() {
// 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)
if err != nil {
log.Fatal(err)
}
fmt.Println(date)
date1, err := time.Parse("2006-01-02", users.Users[i].Date)
if err != nil {
log.Fatal(err)
}
fmt.Println(date1)
// 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 ?????? ")
}
}
}
.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". --->
}
]
}
uj5u.com熱心網友回復:
如果您將日期欄位解組為字串,則需要time.Parse使用與您的日期格式匹配的給定布局來決議它,例如:
date, err := time.Parse("2006/01/02", users.Users[i].Date)
或者,您可以將 usingjson.UnmarshalJSON直接解組到time.Time欄位中,但您需要一些額外的代碼來支持您的自定義日期格式,如json unmarshal time 中所述,該格式不是 RFC 3339 格式。
uj5u.com熱心網友回復:
從 github 下載 araddon timeparse 庫
https://github.com/araddon/dateparse.git
下面是使用這個驚人的包在 golang 中格式化日期的示例,而無需擔心字串日期
t, err := dateparse.ParseAny("3/1/2014")
然后,您可以使用以下函式對其進行格式化。
s:= t.Format("02 Jan 2006")
您絕對可以嘗試使用上述包,它仍然有一些極端情況,但適用于大多數情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/334901.html
上一篇:在YAML檔案中使用環境變數
