TimeZone

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 Time : Amravati Longitude : 82 1/2(.5) degree
Hour : GMT + 5.30

GMT Tamil FD Knowledge

Time Calculations Sample

1000 milliseconds = 1 seconds
1 minute = 60 seconds
1 minute = 60000 milliseconds (minutes to milliseconds = 60 * 1000)
1 hour = 3600  seconds
1 hour = 3600000 milliseconds (hours to milliseconds = 3600 * 1000 )

Therefore, converting minutes to milliseconds involves multiplying by 60 * 1000 = 60000 milliseconds.
converting hours to milliseconds involves multiplying by 3600 * 1000 = 3600000 millliseconds.

UTC vs ISO format for time

I’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

new Date().toISOString()
"2019-11-14T00:55:31.820Z"

new Date().toUTCString()
"Thu, 14 Nov 2019 00:55:16 GMT"

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?

Ans :

Always use ISO 8601 format: 2019-11-14T00:55:31.820Z
Avoid the legacy format of RFC 1123 & 822: Thu, 14 Nov 2019 00:55:16 GMT

UTC & GMT are time-keeping, not formats

UTC and GMT are not formats.

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.

For common business apps, there is no significant difference, literally less than a second’s difference. Most programmers can use the terms interchangeably. If you work for NASA, or the GPS/Galileo navigation projects, then you’ll want to learn more.

ISO 8601

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 “Zulu” per military/aviation tradition.

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.

Always choose ISO 8601 when serializing date-time values as text.

RFC 1123 / RFC 822

Your second example string Thu, 14 Nov 2019 00:55:16 GMT is defined in the older standards RFC 1123 & RFC 822.

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.

Avoid this format whenever possible. Use this only when required for old protocols and systems not yet updated for ISO 8601.

Time zones

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.

Javascript Time Format

let d = new Date();

d = new Date(1164411006456)
//2006-11-24T23:30:06.456Z

d = new Date("2019-08-02T11:30:00+10:00")
//2019-08-02T01:30:00.000Z

d = new Date(2019,7,2,11,30,27,0)
//2019-08-02T06:00:27.000Z

console.log(d.toString());
//Fri Aug 02 2019 11:30:27 GMT+0530 (India Standard Time)

console.log(d.toISOString())
//2019-08-02T06:00:27.000Z

console.log(d.toLocaleString("en-AU"))
// 8/2/2019, 11:30:27 AM

console.log(d.toLocaleString("en-US",{ timeZone: "America/Los_Angeles"}))
// 8/1/2019, 11:00:27 PM

console.log(d.toLocaleString("en-US"))
// 8/2/2019, 11:30:27 AM

console.log(JSON.stringify({myDate: d}))
//{"myDate":"2019-08-02T06:00:27.000Z"}

Moment.js Example 1

{
  "name": "Moment",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "moment": "^2.27.0",
    "moment-timezone": "^0.5.31",
    "nodemon": "^2.0.4"
  }
}
var moment = require('moment-timezone')
//var moment = require('moment')
//moment() - Give moment object with current date and time
let m = moment();

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}

console.log(`moment toString() : ${m.toString()}`)
console.log(`moment toISOString() - UTC : ${m.toISOString()}`)
console.log(`moment toTimeZone() : ${toTimeZone(new Date(), "America/New_York")}`)

console.log(`----------------------------`)

console.log('Local time: '+ new Date())
console.log('Local time toString : '+ new Date().toString())
console.log('Local time toISOString (UTC) : '+ new Date().toISOString())

console.log(`----------------------------`)

var asiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Shanghai"});
console.log('Asia time toISOString: '+ (new Date(asiaTime)).toISOString())

var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
console.log('USA time toISOString: '+ (new Date(usaTime)).toISOString())

var indiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
console.log('India time toISOString: '+ (new Date(indiaTime)).toISOString())


//OUTPUT : 
moment toISOString() - UTC : 2020-09-10T11:43:16.295Z
moment toTimeZone() : 2020/09/10 07:43:16 -0400
----------------------------
Local time: Thu Sep 10 2020 17:13:16 GMT+0530 (India Standard Time)
Local time toString : Thu Sep 10 2020 17:13:16 GMT+0530 (India Standard Time)
Local time toISOString (UTC) : 2020-09-10T11:43:16.305Z
----------------------------
Asia time toISOString: 2020-09-10T14:13:16.000Z
USA time toISOString: 2020-09-10T02:13:16.000Z
India time toISOString: 2020-09-10T11:43:16.000Z

HTML


<body>
	<script src="/momentjs/lib/moment/moment.min.js"></script>
	<script>
		const m = moment();
	</script>
</body>
var moment = require('moment')
//moment() - Give moment object with current date and time
//Create Date + Time 
let m = moment();

// Create from ISO 8601 string
//m = moment("2019-05-19T23:10:00.000+05:00");

// Using a format - Ausralian Format
//m = moment("14/06/2019", "DD/MM/YYY h:mm:A")


// Create using milliseconds since epoch (1st Jan 1970)
//m = moment(600000) //10 miniutes 

// Create using seconds epoch (1st Jan 1970)
//m = moment.unix(7200) //10 miniutes 

// Create a moment object in UTC mode
//m = moment.utc()
//m = moment.utc("2019-05-19T23:10:00.000+05:00")

// console.log(`moment toString() : ${m.toString()}`)
// console.log(`moment toISOString() - UTC : ${m.toISOString()}`)


// console.log(m.get("quarter"))
// console.log(m.quarter())

//console.log(moment())
//console.log(m.minutes(65))
//console.log(m.set("day",10))
// const differentMoment = moment("2020-10-11")
// console.log(moment.min(moment(), differentMoment).toString()) // Find min & max value of moment objects

/* Add Subtract Manipulate */
console.log(`Original Moment : ${m.toString()}`)
//m.add(1, "h").add(16, "minutes")
// m.subtract({
//     "hours": 1,
//     "minutes":15
// })
// m.endOf("month")
// console.log(`After Moment : ${m.toISOString()}`)
// m.utc()
// m.local()
//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

//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
// console.log(`m.utc() : ${m.utc().toString()}`)
// m.utcOffset("+03:00") // Adding 3 Hours to the UTC
// console.log(`After Moment : ${m.toString()}`)

/*Displaying  */

//https://momentjs.com/docs/#/displaying/
//npm moment-with-locales-es6

Reference

Techrepublic
VS01
VS02
Moment.js Tutorials
utc-vs-iso-format-for-time
GMT Tamil FD Knowledge
Moment Timezone Documentation
How to convert date to another timezone in JavaScript ? : geeksforgeeks

Leave a Reply

Your email address will not be published. Required fields are marked *