Professional Documents
Culture Documents
@maggiepint
maggiepint@gmail.com
https://xkcd.com/15
97/
Like Git:
Date and time is complicated
At first, we avoid learning date and time
1 2 3 4 5 6 7 8 9
Point in absolute
time
Coordinated Universal Time (UTC)
A perspective of the global timeline
Allows us to keep track of absolute time
Primary standard by which the world regulates clocks
Defined precisely by scientific community
Includes leap seconds
Has no relationship to geography
Basically the same as GMT, but GMT is not defined precisely by the
scientific community
Local Time
A local time is a perspective of time
It does not represent a point on the global timeline
It is usually not a contiguous timeline (DST)
UTC Time:
2016-04-09T14:17:47Z
What we know What we dont know
IANA time zones are more widely used, and more accurate
Time Zone: America/Chicago
Politics
Time zones are inherently political
Governments change time zones regularly
Governments change time zones suddenly
Russia this year is an example of politicians causing time chaos
http://bit.ly/1SB9TvW
Morocco reverts to standard time during Ramadan
Assume nothing
IANA Time Zone Libraries
JavaScript - Moment TimeZone
http://momentjs.com/timezone/
.NET NodaTime
http://nodatime.org/
Java 8 + - java.time (native)
Java 7 - JodaTime
See Stack Overflow post for more exhaustive list
http://bit.ly/1RUYuuM
Time Zones are not Offsets!
"2016-04-09T19:39:00-05:00
This date could be in:
America/Chicago
America/Bahia_Banderas
America/Bogata
America/Cancun
America/Cayman
And more
ASSUMPTIONS
Things we think we know
Assumption
If you just store everything in UTC,
all your problems will be solved.
A Table With Everything in UTC
Show me all the messages for
the business day of April 4th.
moment('2016-03-12
12:00').add(24,'hour').format('LLL')
"March 13, 2016 1:00 PM"
moment('2016-02-28').add(1, 'year').format('LLL')
"February 28, 2017 12:00 AM"
moment('2016-02-28').add(365, 'days').format('LLL')
"February 27, 2017 12:00 AM"
As Seen on Stack Overflow
var startHours = 8;
var startMinutes = 30;
console.log(elapsedMinutes);
Assumption
Time and date math work in the
same way.
moment('2016-01-01').add(1.5, 'hours').format('LLL')
"January 1, 2016 1:30 AM
moment('2016-01-01').add(1.5, 'days').format('LLL')
"January 3, 2016 12:00 AM"
Time Math vs Date Math
Time math:
Refers to operations involving hours, minutes, seconds, milliseconds
Works
by incrementing or decrementing the position on the global timeline by the
number of units in question
Can use fractional units
Date Math:
Refers to all operations larger than hours days, months, years, quarters, etc.
Works by moving places on the calendar
Cannot be converted to time math
Cannot use fractional units
Assumption
All dates and times exist once in all
time zones
moment('2016-10-16').format('LLL')
http://codeofmatt.com/2013/06/07/javascript-
date-type-is-horribly-broken/
MOMENT.JS
Quality Date and Time in Javascript
Parsing
Consistently parses date values according to your expectations
More than 100 languages supported
Thousands of options for parse-able formats
Parse multiple formats if necessary
moment('2016-12-21T13:25:22').format()
"2016-12-21T13:25:22-06:00"
moment('30/04/2016', 'DD/MM/YYYY').format()
"2016-04-30T00:00:00-05:00"
moment().locale('en-gb').format('L')
"26/04/2016"
moment().format('LLL')
"April 26, 2016 9:28 PM"
moment().locale('ar').format('LLL')
": "
Sane Math
Add and subtract any unit
StartOf/EndOf
Difference
var a = new Date();
a.setDate(a.getDate() - 5);
moment().subtract(5, 'days');
var d = new Date();
var diff = d.getDate() - d.getDay() + (day == 0 ? -6:1);
d.setDate(diff);
d.setHours(0,0,0,0);
moment().startOf('week')
var d1 = new Date();
var d2 = new Date(01/01/2016);
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
var diff = months <= 0 ? 0 : months;
moment().diff('2016-01-01', 'months');
Relative Time
moment().subtract(3, 'months').fromNow()
"3 months ago"
moment().subtract(2, 'seconds').fromNow()
"a few seconds ago"
moment().add(4,
'hours').locale('tlh').fromNow()
"loS rep pIq"
@maggiepint
maggiepint@gmail.com
https://
github.com/maggiepint