How can i add two date forms with time.Parse?

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

How can i add two date forms with time.Parse?

问题

我正在尝试添加两种不同的日期格式以增加测试的灵活性,但是我遇到了以下错误:

当我尝试解析日期时收到的错误消息如下所示:

2021/10/21 12:15:35 parsing time "1982/01/08" as "2006-01-02": cannot parse "/01/08" as "-"

BD.go

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "time"
  9. )
  10. // Users 结构体包含一个用户数组
  11. type Users struct {
  12. Users []User `json:"users"`
  13. }
  14. // User 结构体包含姓名、类型和社交链接列表
  15. type User struct {
  16. Firstname string `json:"fname"`
  17. Secondname string `json:"lname"`
  18. Date string `json:"date"`
  19. }
  20. func Birthday() {
  21. // 打开json文件
  22. jsonFile, err := os.Open("users.json")
  23. // 如果os.Open返回错误,则处理错误
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. fmt.Println("成功打开users.json")
  28. // 延迟关闭json文件,以便稍后解析
  29. defer jsonFile.Close()
  30. // 将打开的json文件读取为字节数组
  31. byteValue, _ := ioutil.ReadAll(jsonFile)
  32. // 初始化Users数组
  33. var users Users
  34. // 将包含json文件内容的字节数组解组为上面定义的'users'
  35. json.Unmarshal(byteValue, &users)
  36. // 遍历users数组中的每个用户,并打印用户类型、姓名和Facebook链接作为示例
  37. // 编写一个函数列出今天过生日的人
  38. // var yr int
  39. for i := 0; i < len(users.Users); i++ {
  40. date, err := time.Parse("2006/01/02", users.Users[i].Date)
  41. date1, _ := time.Parse("2006-01-02", users.Users[i].Date)
  42. if err != nil {
  43. fmt.Println(date)
  44. } else if err != nil {
  45. fmt.Println(date1)
  46. } else {
  47. log.Fatal(err)
  48. }
  49. // 检查日期是否是闰年,例如:29不是闰年,但28是!
  50. if date.Day()%400 == 0 || (date.Day()%4 == 0 && date.Day()%100 != 0) {
  51. fmt.Println("用户名:", users.Users[i].Firstname)
  52. fmt.Println("用户姓氏:", users.Users[i].Secondname)
  53. fmt.Println("用户日期:", users.Users[i].Date)
  54. fmt.Println(users.Users[i].Date, "是闰年 ✨✨✨")
  55. } else if date1.Day()%400 == 0 || (date1.Day()%4 == 0 && date1.Day()%100 != 0) {
  56. fmt.Println("用户名:", users.Users[i].Firstname)
  57. fmt.Println("用户姓氏:", users.Users[i].Secondname)
  58. fmt.Println("用户日期:", users.Users[i].Date)
  59. fmt.Println(users.Users[i].Date, "是闰年 ✨✨✨")
  60. } else {
  61. fmt.Println("用户名:", users.Users[i].Firstname)
  62. fmt.Println("用户姓氏:", users.Users[i].Secondname)
  63. fmt.Println("用户日期:", users.Users[i].Date)
  64. fmt.Println(users.Users[i].Date, "不是闰年 &#128165;&#128165;&#128165;")
  65. }
  66. }
  67. }
  68. func main() {
  69. Birthday()
  70. }

.JSON 文件

  1. {
  2. "users": [
  3. {
  4. "Fname": "Johnny",
  5. "Lname":"mane",
  6. "date":"1982/01/08"
  7. },
  8. {
  9. "Fname": "Wayne",
  10. "Lname":"Bruce",
  11. "date":"1965/01/30"
  12. },
  13. {
  14. "Fname": "Gaga",
  15. "Lname":"Lady",
  16. "date":"1986/03/28"
  17. },
  18. {
  19. "Fname": "radio",
  20. "Lname":"head",
  21. "date":"1988/02/29"
  22. },
  23. {
  24. "Fname": "Mario",
  25. "Lname":"torres",
  26. "date":"1996/09/28"
  27. },
  28. {
  29. "Fname": "robert",
  30. "Lname":"Alex",
  31. "date":"1991/12/01"
  32. },
  33. {
  34. "Fname": "Julia",
  35. "Lname":"sevak",
  36. "date":"1991-03-07"
  37. },
  38. {
  39. "Fname": "feb",
  40. "Lname":"robert",
  41. "date":"1995-01-31"
  42. }
  43. ]
  44. }
英文:

I'm trying to add two different date forms for more flexibility during testing, but I got this error below:

The error I received when I tried to parse dates --> 2021/10/21 12:15:35 parsing time "1982/01/08" as "2006-01-02": cannot parse "/01/08" as "-"

BD.go

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;io/ioutil&quot;
  6. &quot;log&quot;
  7. &quot;os&quot;
  8. &quot;time&quot;
  9. )
  10. // Users struct which contains
  11. // an array of users
  12. type Users struct {
  13. Users []User `json:&quot;users&quot;`
  14. }
  15. // User struct which contains a name
  16. // a type and a list of social links
  17. type User struct {
  18. Firstname string `json:&quot;fname&quot;`
  19. Secondname string `json:&quot;lname&quot;`
  20. Date string `json:&quot;date&quot;`
  21. }
  22. func Birthday() {
  23. // Open our jsonFile
  24. jsonFile, err := os.Open(&quot;users.json&quot;)
  25. // if we os.Open returns an error then handle it
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. fmt.Println(&quot;Successfully Opened users.json&quot;)
  30. // defer the closing of our jsonFile so that we can parse it later on
  31. defer jsonFile.Close()
  32. // read our opened xmlFile as a byte array.
  33. byteValue, _ := ioutil.ReadAll(jsonFile)
  34. // we initialize our Users array
  35. var users Users
  36. // we unmarshal our byteArray which contains our
  37. // jsonFile&#39;s content into &#39;users&#39; which we defined above
  38. json.Unmarshal(byteValue, &amp;users)
  39. // we iterate through every user within our users array and
  40. // print out the user Type, their name, and their facebook url
  41. // as just an example
  42. // write a function to list out the people whose birthday is today.
  43. // var yr int
  44. for i := 0; i &lt; len(users.Users); i++ {
  45. date, err := time.Parse(&quot;2006/01/02&quot;, users.Users[i].Date)
  46. date1, _ := time.Parse(&quot;2006-01-02&quot;, users.Users[i].Date)
  47. if err != nil {
  48. fmt.Println(date)
  49. } else if err != nil {
  50. fmt.Println(date1)
  51. } else {
  52. log.Fatal(err)
  53. }
  54. // check if the date is a leap year, ex: 29 is not a leap year but 28th is !
  55. if date.Day()%400 == 0 || (date.Day()%4 == 0 &amp;&amp; date.Day()%100 != 0) {
  56. fmt.Println(&quot;User First Name: &quot; + users.Users[i].Firstname)
  57. fmt.Println(&quot;User Second Name: &quot; + users.Users[i].Secondname)
  58. fmt.Println(&quot;User Date: &quot; + users.Users[i].Date)
  59. fmt.Println(users.Users[i].Date, &quot; is a Leap Year ✨✨✨ &quot;)
  60. } else if date1.Day()%400 == 0 || (date1.Day()%4 == 0 &amp;&amp; date1.Day()%100 != 0) {
  61. fmt.Println(&quot;User First Name: &quot; + users.Users[i].Firstname)
  62. fmt.Println(&quot;User Second Name: &quot; + users.Users[i].Secondname)
  63. fmt.Println(&quot;User Date: &quot; + users.Users[i].Date)
  64. fmt.Println(users.Users[i].Date, &quot; is a Leap Year ✨✨✨ &quot;)
  65. } else {
  66. fmt.Println(&quot;User First Name: &quot; + users.Users[i].Firstname)
  67. fmt.Println(&quot;User Second Name: &quot; + users.Users[i].Secondname)
  68. fmt.Println(&quot;User Date: &quot; + users.Users[i].Date)
  69. fmt.Println(users.Users[i].Date, &quot; is Not a Leap Year &#128165;&#128165;&#128165; &quot;)
  70. }
  71. }
  72. }
  73. func main() {
  74. Birthday()
  75. }

.JSON file

  1. {
  2. &quot;users&quot;: [
  3. {
  4. &quot;Fname&quot;: &quot;Johnny&quot;,
  5. &quot;Lname&quot;:&quot;mane&quot;,
  6. &quot;date&quot;:&quot;1982/01/08&quot;
  7. },
  8. {
  9. &quot;Fname&quot;: &quot;Wayne&quot;,
  10. &quot;Lname&quot;:&quot;Bruce&quot;,
  11. &quot;date&quot;:&quot;1965/01/30&quot;
  12. },
  13. {
  14. &quot;Fname&quot;: &quot;Gaga&quot;,
  15. &quot;Lname&quot;:&quot;Lady&quot;,
  16. &quot;date&quot;:&quot;1986/03/28&quot;
  17. },
  18. {
  19. &quot;Fname&quot;: &quot;radio&quot;,
  20. &quot;Lname&quot;:&quot;head&quot;,
  21. &quot;date&quot;:&quot;1988/02/29&quot;
  22. },
  23. {
  24. &quot;Fname&quot;: &quot;Mario&quot;,
  25. &quot;Lname&quot;:&quot;torres&quot;,
  26. &quot;date&quot;:&quot;1996/09/28&quot;
  27. },
  28. {
  29. &quot;Fname&quot;: &quot;robert&quot;,
  30. &quot;Lname&quot;:&quot;Alex&quot;,
  31. &quot;date&quot;:&quot;1991/12/01&quot;
  32. },
  33. {
  34. &quot;Fname&quot;: &quot;Julia&quot;,
  35. &quot;Lname&quot;:&quot;sevak&quot;,
  36. &quot;date&quot;:&quot;1991-03-07&quot; --&gt;
  37. },
  38. {
  39. &quot;Fname&quot;: &quot;feb&quot;,
  40. &quot;Lname&quot;:&quot;robert&quot;,
  41. &quot;date&quot;:&quot;1995-01-31&quot;. ---&gt;
  42. }
  43. ]
  44. }

答案1

得分: 3

你的错误处理似乎有问题。你需要在第一次尝试解析日期时检查错误。如果失败了,尝试使用不同的格式解析:

  1. date, err := time.Parse("2006/01/02", users.Users[i].Date)
  2. if err != nil {
  3. // 第一次尝试失败,所以再试一次...
  4. date, err = time.Parse("2006-01-02", users.Users[i].Date)
  5. if err != nil {
  6. // 两次尝试都失败了
  7. }
  8. }
  9. // 一切正常
英文:

Your error handling seems wrong. You need to check for the error on the first attempt parsing the date. If it failed, try parsing with a different layout:

  1. date, err := time.Parse(&quot;2006/01/02&quot;, users.Users[i].Date)
  2. if err != nil {
  3. // first attempt failed, so try again...
  4. date, err = time.Parse(&quot;2006-01-02&quot;, users.Users[i].Date)
  5. if err != nil {
  6. // both attempts failed
  7. }
  8. }
  9. // all ok

huangapple
  • 本文由 发表于 2021年10月21日 16:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69658823.html
匿名

发表评论

匿名网友

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

确定