{"id":1807,"date":"2020-09-10T17:16:42","date_gmt":"2020-09-10T11:46:42","guid":{"rendered":"http:\/\/uitutorials.in\/wp\/?p=1807"},"modified":"2020-09-22T11:35:37","modified_gmt":"2020-09-22T06:05:37","slug":"timezone","status":"publish","type":"post","link":"https:\/\/uitutorials.in\/wp\/timezone\/","title":{"rendered":"TimeZone"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/UTCTimeMap.png\" alt=\"\" width=\"991\" height=\"669\" class=\"alignnone size-full wp-image-1808\" srcset=\"https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/UTCTimeMap.png 991w, https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/UTCTimeMap-300x203.png 300w, https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/UTCTimeMap-768x518.png 768w\" sizes=\"auto, (max-width: 991px) 100vw, 991px\" \/><\/p>\n<p><a href=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/88\/World_Time_Zones_Map.png\" rel=\"noopener\" target=\"_blank\">World Time Zones Map<\/a><\/p>\n<p>Time Calculation : <\/p>\n<p>one day = 24 hours<br \/>\n24 Parts; Circle has 360 degree<\/p>\n<p>360\/24 = 15 degree<\/p>\n<p>Each part has 15 degree Longitude<br \/>\nOne part = 1 Hour<\/p>\n<p>Chennai Longitude : 80.14 Degree<br \/>\n80.14\/15 = 5.34<br \/>\n.34\/100 * 60 = 20.4<br \/>\nso 5.20 Minutes<\/p>\n<p>Indian Time : Amravati Longitude : 82 1\/2(.5) degree<br \/>\nHour : GMT + 5.30<\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=wQ68KcUSPyk\" rel=\"noopener\" target=\"_blank\">GMT Tamil FD Knowledge<\/a><\/p>\n<h3>Time Calculations Sample <\/h3>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">1000 milliseconds = 1 seconds\r\n1 minute = 60 seconds\r\n1 minute = 60000 milliseconds (minutes to milliseconds = 60 * 1000)\r\n1 hour = 3600  seconds\r\n1 hour = 3600000 milliseconds (hours to milliseconds = 3600 * 1000 )<\/code><\/pre>\n<p>Therefore, converting minutes to milliseconds involves multiplying by 60 * 1000 = 60000 milliseconds.<br \/>\nconverting hours to milliseconds involves multiplying by 3600 * 1000 = 3600000 millliseconds.<\/p>\n<h3>UTC vs ISO format for time<\/h3>\n<p>I&#8217;m trying to understand the difference between UTC and ISO formats and when to use what when transferring messages between servers. So When I try the following this is what I get<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">new Date().toISOString()\r\n\"2019-11-14T00:55:31.820Z\"\r\n\r\nnew Date().toUTCString()\r\n\"Thu, 14 Nov 2019 00:55:16 GMT\"<\/code><\/pre>\n<p>I understand the ISO format and its a standard used to represent time, but what is the purpose of UTC and where would I use them?<\/p>\n<h3>Ans : <\/h3>\n<p>Always use ISO 8601 format: 2019-11-14T00:55:31.820Z<br \/>\nAvoid the legacy format of RFC 1123 &#038; 822: Thu, 14 Nov 2019 00:55:16 GMT<\/p>\n<h3>UTC &#038; GMT are time-keeping, not formats<\/h3>\n<p>UTC and GMT are not formats.<\/p>\n<p>UTC and GMT are two slightly different ways of tracking time. This is a complicated topic, so see the Wikipedia pages for the gory details if you really want to know.<\/p>\n<p>For common business apps, there is no significant difference, literally less than a second\u2019s difference. Most programmers can use the terms interchangeably. If you work for NASA, or the GPS\/Galileo navigation projects, then you\u2019ll want to learn more.<\/p>\n<h3>ISO 8601<\/h3>\n<p>The format seen in your first example 2019-11-14T00:55:31.820Z is defined by the ISO 8601 standard. The T in the middle separates the year-month-day portion from the hour-minute-second portion. The Z on the end means UTC, that is, an offset-from-UTC of zero hours-minutes-seconds. The Z is pronounced &#8220;Zulu&#8221; per military\/aviation tradition.<\/p>\n<p>The ISO 8601 standard is more modern. The formats are wisely designed to be easy to parse by machine as well as easy to read by humans across cultures.<\/p>\n<p>Always choose ISO 8601 when serializing date-time values as text.<\/p>\n<h3>RFC 1123 \/ RFC 822<\/h3>\n<p>Your second example string Thu, 14 Nov 2019 00:55:16 GMT is defined in the older standards RFC 1123 &#038; RFC 822.<\/p>\n<p>These are legacy formats. They are terrible, difficult to parse by machine. And they are bad for humans as they assume English language and particular cultural norms.<\/p>\n<p>Avoid this format whenever possible. Use this only when required for old protocols and systems not yet updated for ISO 8601.<\/p>\n<h3>Time zones<\/h3>\n<p>Your example of 2019-11-14T00:55:31.820Z means an offset from UTC of zero hours-minutes seconds. This is the time-of-day and date seen when standing before the clock displayed at the Royal Observatory Greenwich.<\/p>\n<h3>Javascript Time Format <\/h3>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">let d = new Date();\r\n\r\nd = new Date(1164411006456)\r\n\/\/2006-11-24T23:30:06.456Z\r\n\r\nd = new Date(\"2019-08-02T11:30:00+10:00\")\r\n\/\/2019-08-02T01:30:00.000Z\r\n\r\nd = new Date(2019,7,2,11,30,27,0)\r\n\/\/2019-08-02T06:00:27.000Z\r\n\r\nconsole.log(d.toString());\r\n\/\/Fri Aug 02 2019 11:30:27 GMT+0530 (India Standard Time)\r\n\r\nconsole.log(d.toISOString())\r\n\/\/2019-08-02T06:00:27.000Z\r\n\r\nconsole.log(d.toLocaleString(\"en-AU\"))\r\n\/\/ 8\/2\/2019, 11:30:27 AM\r\n\r\nconsole.log(d.toLocaleString(\"en-US\",{ timeZone: \"America\/Los_Angeles\"}))\r\n\/\/ 8\/1\/2019, 11:00:27 PM\r\n\r\nconsole.log(d.toLocaleString(\"en-US\"))\r\n\/\/ 8\/2\/2019, 11:30:27 AM\r\n\r\nconsole.log(JSON.stringify({myDate: d}))\r\n\/\/{\"myDate\":\"2019-08-02T06:00:27.000Z\"}<\/code><\/pre>\n<h2>Moment.js Example 1<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">{\r\n  \"name\": \"Moment\",\r\n  \"version\": \"1.0.0\",\r\n  \"description\": \"\",\r\n  \"main\": \"index.js\",\r\n  \"scripts\": {\r\n    \"start\": \"nodemon index.js\"\r\n  },\r\n  \"keywords\": [],\r\n  \"author\": \"\",\r\n  \"license\": \"ISC\",\r\n  \"dependencies\": {\r\n    \"moment\": \"^2.27.0\",\r\n    \"moment-timezone\": \"^0.5.31\",\r\n    \"nodemon\": \"^2.0.4\"\r\n  }\r\n}\r\n<\/code><\/pre>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">var moment = require('moment-timezone')\r\n\/\/var moment = require('moment')\r\n\/\/moment() - Give moment object with current date and time\r\nlet m = moment();\r\n\r\nfunction toTimeZone(time, zone) {\r\n    var format = 'YYYY\/MM\/DD HH:mm:ss ZZ';\r\n    return moment(time, format).tz(zone).format(format);\r\n}\r\n\r\nconsole.log(`moment toString() : ${m.toString()}`)\r\nconsole.log(`moment toISOString() - UTC : ${m.toISOString()}`)\r\nconsole.log(`moment toTimeZone() : ${toTimeZone(new Date(), \"America\/New_York\")}`)\r\n\r\nconsole.log(`----------------------------`)\r\n\r\nconsole.log('Local time: '+ new Date())\r\nconsole.log('Local time toString : '+ new Date().toString())\r\nconsole.log('Local time toISOString (UTC) : '+ new Date().toISOString())\r\n\r\nconsole.log(`----------------------------`)\r\n\r\nvar asiaTime = new Date().toLocaleString(\"en-US\", {timeZone: \"Asia\/Shanghai\"});\r\nconsole.log('Asia time toISOString: '+ (new Date(asiaTime)).toISOString())\r\n\r\nvar usaTime = new Date().toLocaleString(\"en-US\", {timeZone: \"America\/New_York\"});\r\nconsole.log('USA time toISOString: '+ (new Date(usaTime)).toISOString())\r\n\r\nvar indiaTime = new Date().toLocaleString(\"en-US\", {timeZone: \"Asia\/Kolkata\"});\r\nconsole.log('India time toISOString: '+ (new Date(indiaTime)).toISOString())\r\n\r\n\r\n\/\/OUTPUT : \r\nmoment toISOString() - UTC : 2020-09-10T11:43:16.295Z\r\nmoment toTimeZone() : 2020\/09\/10 07:43:16 -0400\r\n----------------------------\r\nLocal time: Thu Sep 10 2020 17:13:16 GMT+0530 (India Standard Time)\r\nLocal time toString : Thu Sep 10 2020 17:13:16 GMT+0530 (India Standard Time)\r\nLocal time toISOString (UTC) : 2020-09-10T11:43:16.305Z\r\n----------------------------\r\nAsia time toISOString: 2020-09-10T14:13:16.000Z\r\nUSA time toISOString: 2020-09-10T02:13:16.000Z\r\nIndia time toISOString: 2020-09-10T11:43:16.000Z\r\n<\/code><\/pre>\n<h3>HTML<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\r\n&lt;body&gt;\r\n\t&lt;script src=\"\/momentjs\/lib\/moment\/moment.min.js\"&gt;&lt;\/script&gt;\r\n\t&lt;script&gt;\r\n\t\tconst m = moment();\r\n\t&lt;\/script&gt;\r\n&lt;\/body&gt;<\/code><\/pre>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">var moment = require('moment')\r\n\/\/moment() - Give moment object with current date and time\r\n\/\/Create Date + Time \r\nlet m = moment();\r\n\r\n\/\/ Create from ISO 8601 string\r\n\/\/m = moment(\"2019-05-19T23:10:00.000+05:00\");\r\n\r\n\/\/ Using a format - Ausralian Format\r\n\/\/m = moment(\"14\/06\/2019\", \"DD\/MM\/YYY h:mm:A\")\r\n\r\n\r\n\/\/ Create using milliseconds since epoch (1st Jan 1970)\r\n\/\/m = moment(600000) \/\/10 miniutes \r\n\r\n\/\/ Create using seconds epoch (1st Jan 1970)\r\n\/\/m = moment.unix(7200) \/\/10 miniutes \r\n\r\n\/\/ Create a moment object in UTC mode\r\n\/\/m = moment.utc()\r\n\/\/m = moment.utc(\"2019-05-19T23:10:00.000+05:00\")\r\n\r\n\/\/ console.log(`moment toString() : ${m.toString()}`)\r\n\/\/ console.log(`moment toISOString() - UTC : ${m.toISOString()}`)\r\n\r\n\r\n\/\/ console.log(m.get(\"quarter\"))\r\n\/\/ console.log(m.quarter())\r\n\r\n\/\/console.log(moment())\r\n\/\/console.log(m.minutes(65))\r\n\/\/console.log(m.set(\"day\",10))\r\n\/\/ const differentMoment = moment(\"2020-10-11\")\r\n\/\/ console.log(moment.min(moment(), differentMoment).toString()) \/\/ Find min & max value of moment objects\r\n\r\n\/* Add Subtract Manipulate *\/\r\nconsole.log(`Original Moment : ${m.toString()}`)\r\n\/\/m.add(1, \"h\").add(16, \"minutes\")\r\n\/\/ m.subtract({\r\n\/\/     \"hours\": 1,\r\n\/\/     \"minutes\":15\r\n\/\/ })\r\n\/\/ m.endOf(\"month\")\r\n\/\/ console.log(`After Moment : ${m.toISOString()}`)\r\n\/\/ m.utc()\r\n\/\/ m.local()\r\n\/\/console.log(`Offset Moment : ${m.utcOffset()} :  +${ m.utcOffset() \/ 60}` ) \/\/ 330 - its 330 minutes past UTC, if we do for m.utc(). It will output 0\r\n\r\n\/\/m.utcOffset(17) \/\/-15 to 15 will be considered as hours , when you do anything past that number it's going to interpret it as minutes thats why we putting 3 or 300 it soon give us same result\r\n\/\/ console.log(`m.utc() : ${m.utc().toString()}`)\r\n\/\/ m.utcOffset(\"+03:00\") \/\/ Adding 3 Hours to the UTC\r\n\/\/ console.log(`After Moment : ${m.toString()}`)\r\n\r\n\/*Displaying  *\/\r\n\r\n\/\/https:\/\/momentjs.com\/docs\/#\/displaying\/\r\n\/\/npm moment-with-locales-es6\r\n<\/code><\/pre>\n<h3>Reference<\/h3>\n<p><a href=\"https:\/\/www.techrepublic.com\/article\/convert-the-local-time-to-another-time-zone-with-this-javascript\/\" rel=\"noopener\" target=\"_blank\">Techrepublic<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=oKFb2Us9kmg\" rel=\"noopener\" target=\"_blank\">VS01<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=UT2dP447roo\" rel=\"noopener\" target=\"_blank\">VS02<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/playlist?list=PLVvjrrRCBy2LWFkR7opQxWp4z0en6OHgw\" rel=\"noopener\" target=\"_blank\">Moment.js Tutorials<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/58847869\/utc-vs-iso-format-for-time\" rel=\"noopener\" target=\"_blank\">utc-vs-iso-format-for-time<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=wQ68KcUSPyk\" rel=\"noopener\" target=\"_blank\">GMT Tamil FD Knowledge<\/a><br \/>\n<a href=\"https:\/\/momentjs.com\/timezone\/docs\/\" rel=\"noopener\" target=\"_blank\">Moment Timezone Documentation<\/a><br \/>\n<a href=\"https:\/\/www.geeksforgeeks.org\/how-to-convert-date-to-another-timezone-in-javascript\/\" rel=\"noopener\" target=\"_blank\">How to convert date to another timezone in JavaScript ? : geeksforgeeks <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>World Time Zones Map Time Calculation : one day = 24 hours 24 Parts; Circle has 360 degree 360\/24 = 15 degree Each part has 15 degree Longitude One part = 1 Hour Chennai Longitude : 80.14 Degree 80.14\/15 = 5.34 .34\/100 * 60 = 20.4 so 5.20 Minutes Indian<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,1],"tags":[100],"class_list":["post-1807","post","type-post","status-publish","format-standard","hentry","category-javascript","category-nodejs","tag-datetime","ct-col-2"],"_links":{"self":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/1807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/comments?post=1807"}],"version-history":[{"count":9,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/1807\/revisions"}],"predecessor-version":[{"id":1828,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/1807\/revisions\/1828"}],"wp:attachment":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/media?parent=1807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/categories?post=1807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/tags?post=1807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}