{"id":3922,"date":"2024-11-10T09:12:44","date_gmt":"2024-11-10T01:12:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3922\/"},"modified":"2024-11-10T09:12:44","modified_gmt":"2024-11-10T01:12:44","slug":"%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e8%ae%a1%e7%ae%97-javascript-%e6%97%b6%e9%97%b4%e5%b7%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3922\/","title":{"rendered":"\u5982\u4f55\u6b63\u786e\u8ba1\u7b97 JavaScript \u65f6\u95f4\u5dee\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173116440533287.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u6b63\u786e\u8ba1\u7b97 JavaScript \u65f6\u95f4\u5dee\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u6b63\u786e\u8ba1\u7b97 JavaScript \u65f6\u95f4\u5dee\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u8ba1\u7b97 javascript \u65f6\u95f4\u5dee<\/strong><\/p>\n<p>\u5728\u8ba1\u7b97 javascript \u65f6\u95f4\u5dee\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u65e5\u671f\u683c\u5f0f\u5316\u4ee5\u53ca\u5355\u4f4d\u8f6c\u6362\u3002<\/p>\n<p><strong>\u539f\u59cb\u4ee3\u7801\u5b58\u5728\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u63d0\u4f9b\u7684\u4ee3\u7801\u7684\u95ee\u9898\u5728\u4e8e\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li> <strong>\u65f6\u95f4\u5dee\u8ba1\u7b97\u65b9\u5f0f\u4e0d\u6b63\u786e\uff1a<\/strong>enddate.gettime() &#8211; begindate.gettime() \u5f97\u5230\u7684\u662f\u6beb\u79d2\u7ea7\u7684\u65f6\u95f4\u5dee\uff0c\u65e0\u6cd5\u76f4\u63a5\u8f6c\u5316\u4e3a\u5b8c\u6574\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/li>\n<li> <strong>\u65f6\u95f4\u683c\u5f0f\u5316\u4e0d\u6b63\u786e\uff1a<\/strong>\u4f7f\u7528 new date(diff) \u521b\u5efa\u65b0\u7684\u65e5\u671f\u5bf9\u8c61\u65f6\uff0cdiff \u65f6\u95f4\u5dee\u662f\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u7684\uff0c\u56e0\u6b64\u4f1a\u5f97\u5230\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/li>\n<li> <strong>\u6ca1\u6709\u8003\u8651\u65f6\u533a\uff1a<\/strong>\u672a\u8003\u8651\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u65f6\u533a\u5dee\u5f02\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8ba1\u7b97\u7ed3\u679c\u4e0d\u51c6\u786e\u3002<\/li>\n<\/ol>\n<p><strong>\u6b63\u786e\u7684\u8ba1\u7b97\u65b9\u5f0f<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u63d0\u4f9b\u4e86\u8ba1\u7b97 javascript \u65f6\u95f4\u5dee\u7684\u6b63\u786e\u65b9\u5f0f\uff1a<\/p>\n<pre>var new_date = new Date(); \/\/\u65b0\u5efa\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u73b0\u5728\u7684\u65f6\u95f4\nvar old_date = new Date(\"2021-08-03 09:20:49\"); \/\/\u8bbe\u7f6e\u8fc7\u53bb\u7684\u4e00\u4e2a\u65f6\u95f4\u70b9\uff0c\"yyyy-MM-dd HH:mm:ss\"\u683c\u5f0f\u5316\u65e5\u671f\nvar difftime = (new_date - old_date)\/1000; \/\/\u8ba1\u7b97\u65f6\u95f4\u5dee,\u5e76\u628a\u6beb\u79d2\u8f6c\u6362\u6210\u79d2\nvar days = parseInt(difftime\/86400); \/\/ \u5929  24*60*60*1000\nvar hours = parseInt(difftime\/3600)-24*days;    \/\/ \u5c0f\u65f6 60*60 \u603b\u5c0f\u65f6\u6570-\u8fc7\u53bb\u7684\u5c0f\u65f6\u6570=\u73b0\u5728\u7684\u5c0f\u65f6\u6570\nvar minutes = parseInt(difftime%3600\/60); \/\/ \u5206\u949f -(day*24) \u4ee560\u79d2\u4e3a\u4e00\u6574\u4efd \u53d6\u4f59 \u5269\u4e0b\u79d2\u6570 \u79d2\u6570\/60 \u5c31\u662f\u5206\u949f\u6570\nvar seconds = parseInt(difftime%60);  \/\/ \u4ee560\u79d2\u4e3a\u4e00\u6574\u4efd \u53d6\u4f59 \u5269\u4e0b\u79d2\u6570<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u4ee3\u7801\u5c06\u65f6\u95f4\u5dee\u4ee5\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u65b9\u5f0f\u5448\u73b0\u3002\u901a\u8fc7\u5c06 difftime \u9664\u4ee5\u9002\u5f53\u7684\u5355\u4f4d\uff08\u4f8b\u5982 86400 \u79d2\/\u5929\uff09\u5e76\u53d6\u6574\uff0c\u53ef\u4ee5\u5f97\u5230\u76f8\u5e94\u7684\u5dee\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u6b63\u786e\u8ba1\u7b97 JavaScript \u65f6\u95f4\u5dee\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba1\u7b97 javascript \u65f6\u95f4\u5dee \u5728\u8ba1\u7b97 javascript \u65f6\u95f4\u5dee\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u65e5\u671f\u683c\u5f0f\u5316\u4ee5\u53ca\u5355\u4f4d\u8f6c\u6362\u3002 \u539f\u59cb\u4ee3\u7801\u5b58\u5728\u7684\u95ee\u9898 \u63d0\u4f9b\u7684\u4ee3\u7801\u7684\u95ee\u9898\u5728\u4e8e\u4ee5\u4e0b\u51e0\u70b9\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u65f6\u95f4\u5dee\u8ba1\u7b97\u65b9\u5f0f\u4e0d\u6b63\u786e\uff1aenddate.gettime() &#8211; begindate.gettime() \u5f97\u5230\u7684\u662f\u6beb\u79d2\u7ea7\u7684\u65f6\u95f4\u5dee\uff0c\u65e0\u6cd5\u76f4\u63a5\u8f6c\u5316\u4e3a\u5b8c\u6574\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002 \u65f6\u95f4\u683c\u5f0f\u5316\u4e0d\u6b63\u786e\uff1a\u4f7f\u7528 new date(diff) \u521b\u5efa\u65b0\u7684\u65e5\u671f\u5bf9\u8c61\u65f6\uff0cdiff \u65f6\u95f4\u5dee\u662f\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u7684\uff0c\u56e0\u6b64\u4f1a\u5f97\u5230\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002 \u6ca1\u6709\u8003\u8651\u65f6\u533a\uff1a\u672a\u8003\u8651\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u65f6\u533a\u5dee\u5f02\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8ba1\u7b97\u7ed3\u679c\u4e0d\u51c6\u786e\u3002 \u6b63\u786e\u7684\u8ba1\u7b97\u65b9\u5f0f \u4ee5\u4e0b\u4ee3\u7801\u63d0\u4f9b\u4e86\u8ba1\u7b97 javascript \u65f6\u95f4\u5dee\u7684\u6b63\u786e\u65b9\u5f0f\uff1a var new_date = new Date(); \/\/\u65b0\u5efa\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u73b0\u5728\u7684\u65f6\u95f4 var old_date = new Date(&#8220;2021-08-03 09:20:49&#8221;); \/\/\u8bbe\u7f6e\u8fc7\u53bb\u7684\u4e00\u4e2a\u65f6\u95f4\u70b9\uff0c&#8221;yyyy-MM-dd HH:mm:ss&#8221;\u683c\u5f0f\u5316\u65e5\u671f var difftime = (new_date &#8211; old_date)\/1000; \/\/\u8ba1\u7b97\u65f6\u95f4\u5dee,\u5e76\u628a\u6beb\u79d2\u8f6c\u6362\u6210\u79d2 var days = parseInt(difftime\/86400); \/\/ \u5929 24*60*60*1000 var hours = parseInt(difftime\/3600)-24*days; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3922","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3922"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3922\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}