{"id":1004,"date":"2025-01-16T00:48:11","date_gmt":"2025-01-15T16:48:11","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1004"},"modified":"2025-01-16T00:48:11","modified_gmt":"2025-01-15T16:48:11","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-go-%e7%9a%84-time-time-%e8%bd%ac%e6%8d%a2%e4%b8%ba%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%a0%bc%e5%bc%8f%e5%b9%b6%e5%ad%98%e5%82%a8%e5%88%b0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/16\/%e5%a6%82%e4%bd%95%e5%b0%86-go-%e7%9a%84-time-time-%e8%bd%ac%e6%8d%a2%e4%b8%ba%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%a0%bc%e5%bc%8f%e5%b9%b6%e5%ad%98%e5%82%a8%e5%88%b0\/","title":{"rendered":"\u5982\u4f55\u5c06 Go \u7684\u00a0time.Time\u00a0\u8f6c\u6362\u4e3a\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u683c\u5f0f\u5e76\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\uff0c\u5c06 <code>time.Time<\/code> \u7c7b\u578b\u8f6c\u6362\u4e3a\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u683c\u5f0f\u4ee5\u4fbf\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528 <code>time.Format<\/code> \u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u65f6\u95f4\u683c\u5f0f\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u57fa\u672c\u6982\u5ff5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>time.Time<\/code> \u662f Go \u4e2d\u8868\u793a\u65f6\u95f4\u7684\u4e3b\u8981\u7c7b\u578b\uff0c<code>time.Format<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u65f6\u95f4\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<code>time.Format<\/code> \u65b9\u6cd5\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u7279\u5b9a\u7684\u5e03\u5c40\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e86\u65f6\u95f4\u7684\u8f93\u51fa\u683c\u5f0f\u3002Go \u4f7f\u7528\u56fa\u5b9a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u6837\u5f0f <code>\"Mon Jan 2 15:04:05 2006\"<\/code> \u6765\u786e\u5b9a\u683c\u5f0f\uff0c\u8fd9\u4e2a\u6837\u5f0f\u88ab\u79f0\u4e3a Go \u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u5e0c\u671b\u5c06 <code>time.Time<\/code> \u8f6c\u6362\u4e3a <code>YYYY-MM-DD HH:mm:ss<\/code> \u683c\u5f0f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    \/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\n    currentTime := time.Now()\n\n    \/\/ \u5b9a\u4e49\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f\n    \/\/ \u4f7f\u7528 Go \u7684\u65f6\u95f4\u5e03\u5c40\u5f62\u5f0f\uff0c'2006-01-02 15:04:05' \u4ee3\u8868 'YYYY-MM-DD HH:mm:ss'\n    format := \"2006-01-02 15:04:05\"\n\n    \/\/ \u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\n    formattedTime := currentTime.Format(format)\n\n    \/\/ \u8f93\u51fa\u683c\u5f0f\u5316\u540e\u7684\u65f6\u95f4\n    fmt.Println(\"Formatted time:\", formattedTime)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Formatted time: 2025-01-16 14:25:30<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5e38\u89c1\u65f6\u95f4\u683c\u5f0f\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65f6\u95f4\u683c\u5f0f\u548c\u5bf9\u5e94\u7684 Go \u65f6\u95f4\u5e03\u5c40\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>YYYY-MM-DD<\/strong>\uff1a<code>\"2006-01-02\"<\/code><\/li>\n\n\n\n<li><strong>YYYY\/MM\/DD<\/strong>\uff1a<code>\"2006\/01\/02\"<\/code><\/li>\n\n\n\n<li><strong>HH:mm:ss<\/strong>\uff1a<code>\"15:04:05\"<\/code><\/li>\n\n\n\n<li><strong>YYYY-MM-DD HH:mm:ss<\/strong>\uff1a<code>\"2006-01-02 15:04:05\"<\/code><\/li>\n\n\n\n<li><strong>MM-DD-YYYY<\/strong>\uff1a<code>\"01-02-2006\"<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u5c06\u683c\u5f0f\u5316\u540e\u7684\u65f6\u95f4\u5b58\u50a8\u5230\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5b58\u50a8\u3002\u5047\u8bbe\u4f7f\u7528 <code>SQL<\/code> \u6570\u636e\u5e93\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u76f4\u63a5\u63d2\u5165\u5230\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>database\/sql<\/code> \u5305\u6267\u884c\u63d2\u5165\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"database\/sql\"\n    \"fmt\"\n    \"log\"\n    \"time\"\n\n    _ \"github.com\/go-sql-driver\/mysql\"\n)\n\nfunc main() {\n    \/\/ \u5047\u8bbe\u6570\u636e\u5e93\u8fde\u63a5\n    dsn := \"username:password@tcp(localhost:3306)\/yourdb\"\n    db, err := sql.Open(\"mysql\", dsn)\n    if err != nil {\n        log.Fatal(err)\n    }\n    defer db.Close()\n\n    \/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u683c\u5f0f\u5316\u4e3a\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\n    currentTime := time.Now()\n    formattedTime := currentTime.Format(\"2006-01-02 15:04:05\")\n\n    \/\/ \u6267\u884c\u6570\u636e\u5e93\u63d2\u5165\u64cd\u4f5c\n    _, err = db.Exec(\"INSERT INTO your_table (your_column) VALUES (?)\", formattedTime)\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    fmt.Println(\"Time inserted successfully:\", formattedTime)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u81ea\u5b9a\u4e49\u65e5\u671f\u683c\u5f0f\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u65f6\u533a\u95ee\u9898<\/strong>\uff1a\u5728\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u786e\u4fdd\u8003\u8651\u65f6\u533a\u95ee\u9898\u3002\u5982\u679c\u6570\u636e\u5e93\u9700\u8981\u5b58\u50a8 UTC \u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>time.UTC()<\/code> \u6765\u786e\u4fdd\u65f6\u95f4\u662f UTC\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5e93\u683c\u5f0f<\/strong>\uff1a\u4e0d\u540c\u7684\u6570\u636e\u5e93\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\u8981\u6c42\u3002\u4f8b\u5982\uff0cMySQL \u4f7f\u7528 <code>DATETIME<\/code> \u7c7b\u578b\u6765\u5b58\u50a8\u65e5\u671f\u65f6\u95f4\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f <code>YYYY-MM-DD HH:mm:ss<\/code> \u6765\u63d2\u5165\u6570\u636e\u3002\u5982\u679c\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u8bf7\u6839\u636e\u6570\u636e\u5e93\u7684\u8981\u6c42\u8c03\u6574\u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u4f7f\u7528\u6570\u636e\u5e93\u5b57\u6bb5\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5728\u6570\u636e\u5e93\u4e2d\u4f7f\u7528\u7684\u662f <code>DATE<\/code>, <code>DATETIME<\/code>, <code>TIMESTAMP<\/code> \u7b49\u7c7b\u578b\uff0c\u8bb8\u591a\u6570\u636e\u5e93\u63d0\u4f9b\u4e86\u5185\u5efa\u7684\u65e5\u671f\u65f6\u95f4\u8f6c\u6362\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5c06 <code>time.Time<\/code> \u7c7b\u578b\u7684\u6570\u636e\u4f20\u8f93\u5230\u6570\u636e\u5e93\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u683c\u5f0f\u5316\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u4f7f\u7528 <code>gorm<\/code> ORM \u5e93\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5904\u7406\u65f6\u95f4\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n\n    \"gorm.io\/driver\/sqlite\"\n    \"gorm.io\/gorm\"\n)\n\ntype Event struct {\n    ID        uint\n    CreatedAt time.Time\n}\n\nfunc main() {\n    \/\/ \u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\n    db, err := gorm.Open(sqlite.Open(\"gorm.db\"), &amp;gorm.Config{})\n    if err != nil {\n        fmt.Println(\"Error connecting to database:\", err)\n        return\n    }\n\n    \/\/ \u81ea\u52a8\u8fc1\u79fb\u6570\u636e\u5e93\u8868\u7ed3\u6784\n    db.AutoMigrate(&amp;Event{})\n\n    \/\/ \u63d2\u5165\u4e8b\u4ef6\n    event := Event{CreatedAt: time.Now()}\n    db.Create(&amp;event)\n\n    \/\/ \u6253\u5370\u63d2\u5165\u65f6\u95f4\n    fmt.Println(\"Event created at:\", event.CreatedAt)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 <code>gorm<\/code> \u65f6\uff0c<code>time.Time<\/code> \u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u6570\u636e\u5e93\u4e2d\u9002\u5f53\u7684 <code>DATETIME<\/code> \u6216 <code>TIMESTAMP<\/code> \u683c\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\u5c06 <code>time.Time<\/code> \u8f6c\u6362\u4e3a\u6570\u636e\u5e93\u5b58\u50a8\u7684\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528 <code>time.Format<\/code> \u65b9\u6cd5\u6765\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u5b9a\u4e49\u65e5\u671f\u548c\u65f6\u95f4\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u9700\u8981\u6ce8\u610f\u65f6\u95f4\u683c\u5f0f\u7684\u5339\u914d\uff0c\u786e\u4fdd\u6b63\u786e\u5b58\u50a8\u548c\u8bfb\u53d6\u65f6\u95f4\u6570\u636e\u3002\u5982\u679c\u4f7f\u7528 ORM\uff0c\u5982 <code>gorm<\/code>\uff0c\u5b83\u4f1a\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u4e2d\uff0c\u5c06 time.Time \u7c7b\u578b\u8f6c\u6362\u4e3a\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u683c\u5f0f\u4ee5\u4fbf\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528 time.F [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-1004","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1004"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1004\/revisions"}],"predecessor-version":[{"id":1005,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1004\/revisions\/1005"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}