{"id":10006952,"date":"2022-12-14T17:02:36","date_gmt":"2022-12-14T09:02:36","guid":{"rendered":"https:\/\/ityet.com\/?p=10006952"},"modified":"2022-12-14T17:02:36","modified_gmt":"2022-12-14T09:02:36","slug":"%e6%89%8b%e5%86%99%e4%b8%80%e4%b8%aa%e5%90%8c%e6%ad%a5%e6%9c%8d%e5%8a%a1%e7%ab%af%e6%97%b6%e9%97%b4%e7%9a%84%e5%b0%8f%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006952","title":{"rendered":"\u624b\u5199\u4e00\u4e2a\u540c\u6b65\u670d\u52a1\u7aef\u65f6\u95f4\u7684\u5c0f\u5de5\u5177"},"content":{"rendered":"<p>\u5728\u524d\u7aef\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u7ecf\u5e38\u4f1a\u7528\u5230 new Date() \u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u4f46\u662f new Date() \u662f\u83b7\u53d6\u7684\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u65f6\u95f4\uff0c\u7531\u4e8e\u7528\u6237\u53ef\u4ee5\u4fee\u6539\u5f53\u524d\u7535\u8111\u65f6\u95f4\uff0c\u6240\u4ee5\u5b83\u662f\u4e0d\u51c6\u786e\u7684\u3002<\/p>\n<p>\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u4fee\u6539\u5f53\u524d\u7535\u8111\u65f6\u95f4\u90fd\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u662f\u5f53\u6211\u4eec\u9700\u8981\u6839\u636e\u670d\u52a1\u7aef\u4f20\u9012\u7684\u6570\u636e\u65f6\u95f4\u4e0e\u5f53\u524d\u65f6\u95f4\u8fdb\u884c\u8ba1\u7b97\u65f6\uff0c\u524d\u7aef\u5c55\u793a\u5c31\u4f1a\u51fa\u9519\u3002\u540c\u65f6\uff0c\u9700\u8981\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\uff08\u65f6\u95f4\uff09\u5b58\u5165\u524d\u7aef\u7f13\u5b58\uff08 localStorage, IndexedDB \uff09\u4e2d\u4e5f\u662f\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u6211\u4eec\u8003\u8651\u4f7f\u7528\u670d\u52a1\u5668\u63d0\u4f9b\u7684\u65f6\u95f4\uff0c\u800c\u4e0d\u662f\u524d\u7aef\u65f6\u95f4\u3002\u670d\u52a1\u5668\u6bcf\u6b21\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u65f6\u90fd\u4f1a\u5728\u54cd\u5e94\u5934\u63d0\u4f9b\u65f6\u95f4\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8be5\u6570\u636e\u4fee\u6b63\u524d\u7aef\u65f6\u95f4\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc4BrR\" alt=\"sync-time\" title=\"sync-time\" loading=\"lazy\"><\/p>\n<p>\u4e8e\u662f\u4e2a\u4eba\u5199\u4e86\u4e00\u4e2a\u5c0f\u5de5\u5177 <a href=\"https:\/\/link.segmentfault.com\/?enc=xCLRl0iE80lcwq9BEZE3UA%3D%3D.rzsGWJk11oRtoTXFsb0jVn5Kdq%2F5V4sZlmYn6h6qeHWM1dbZQj%2BRN7HxBoOLejzo\" rel=\"nofollow\">sync-time<\/a> \u3002\u4ee5 fetch \u4e3a\u4f8b\u5b50:<\/p>\n<pre><code class=\"ts\">import { sync, time, date } from 'sync-time'\n\nasync function getJSON() {\n  let url = 'https:\/\/www.npmjs.com\/search?q=';\n  let response\n  try {\n    response = await fetch(url);\n\n    \/\/ \u54cd\u5e94\u5934\u90e8\u901a\u5e38\u4f1a\u6709 date \u6570\u636e\n    console.log(response.headers.get('date'))\n\n    \/\/ \u628a\u54cd\u5e94\u5934\u65f6\u95f4\u4f5c\u4e3a\u670d\u52a1\u5668\u65f6\u95f4\uff0c\u8c03\u7528 sync \u540c\u6b65\u6570\u636e\n    sync(response.headers.get('date'))\n  } catch (error) {\n  }\n  return response.body\n}\n\ngetJson()\n\n\/\/ =&gt; \u8fd4\u56de\u6570\u5b57\uff0c\u5373\u4fee\u6b63\u597d\u7684\u6beb\u79d2 getTime\ntime()\n\/\/ 1670345143730\n\n\/\/ \u8fd4\u56de Date\uff0cnew Date(time())\ndate()\n\/\/ Wed Dec 07 2022 00:46:47 GMT+0800 (\u4e2d\u56fd\u6807\u51c6\u65f6\u95f4)<\/code><\/pre>\n<p>\u6e90\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"ts\">let diffMillisecond: number = 0\n\n\/\/ \u83b7\u53d6\u524d\u7aef\u65f6\u95f4\nconst getCurrentTime = (): number =&gt; (new Date()).getTime();\n\n\/\/ \u540c\u6b65\u65f6\u95f4\nconst sync = (time: Date | string): void =&gt; {\n  \/\/ \u6ca1\u6709\u4f20\u9012\u65f6\u95f4\uff0c\u76f4\u63a5\u4f7f\u7528\u524d\u7aef\u65f6\u95f4\n  if (!time) {\n    diffMillisecond = 0\n    return\n  }\n\n  \/\/ \u83b7\u53d6 UNIX \u65f6\u95f4\u6233\n  const syncTime = time instanceof Date ? time.getTime() : Date.parse(time)\n  \n  \/\/ \u5f53\u524d\u662f NaN\uff0c\u76f4\u63a5\u8fd4\u56de\n  if (Number.isNaN(syncTime)) {\n    return\n  }\n\n  \/\/ \u83b7\u53d6\u4e24\u4e2a\u65f6\u95f4\u7684\u5dee\u503c\n  diffMillisecond = syncTime - getCurrentTime()\n}\n\n\/\/ \u8865\u5dee\u503c\u5e76\u83b7\u53d6 UNIX \u65f6\u95f4\u6233\nconst time = (): number =&gt; getCurrentTime() + diffMillisecond\n\nconst date = (): Date =&gt; new Date(time())\n\nexport {\n  sync,\n  time,\n  date\n}<\/code><\/pre>\n<h2>\u9f13\u52b1\u4e00\u4e0b<\/h2>\n<p>\u5982\u679c\u4f60\u89c9\u5f97\u8fd9\u7bc7\u6587\u7ae0\u4e0d\u9519\uff0c\u5e0c\u671b\u53ef\u4ee5\u7ed9\u4e0e\u6211\u4e00\u4e9b\u9f13\u52b1\uff0c\u5728\u6211\u7684 github \u535a\u5ba2\u4e0b\u5e2e\u5fd9 star \u4e00\u4e0b\u3002 <\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=GKv6OTGeZ13acV7uEgfEuw%3D%3D.K0doWcjTlHQRVR5sFlcBZRMPi7h16JtjpRyfdDPz83X2J8F0lZD4qkc8X1cXAxUS\" rel=\"nofollow\">\u535a\u5ba2\u5730\u5740<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043003926\">https:\/\/segmentfault.com\/a\/1190000043003926<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u7aef\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u7ecf\u5e38\u4f1a\u7528\u5230 new Date() \u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u4f46\u662f new Date() \u662f\u83b7\u53d6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006952","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006952"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006952\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}