SlideShare une entreprise Scribd logo
1  sur  46
About Me
www.maggiepint.com
@maggiepint
maggiepint@gmail.com
var a = new Date('2016-01-04');
a.toString();
" Sun Jan 03 2016 16:00:00 GMT-0800 (PST) "
var a = new Date('1/4/2016');
a.toString();
" Mon Jan 04 2016 00:00:00 GMT-0800 (PST) "
Brokens!
1. UTC and Local Context Switches
2. NoTime Zone Support
3. Parsing (all of it)
• The spec is a disaster
4. Formatting
5. Math APIs
MOMENT.JS
Quality Date andTime in Javascript
Facts
• Primary Date andTime Library for JavaScript
• Community Based
• 5Years Old
• 5 Core Maintainers and 1 Author Emeritus
• All part-time, unpaid
• No Corporate backing
• 28,157 Stars
• 3,961 Forks
• 6,676,292 NPM Downloads In the Last Month
• 10th Most Depended Upon NPM Package
FIXING DATE
10 Days for aWhole Language
BROKEN 1: UTC AND
LOCAL
5 O'clock Somewhere
PERSPECTIVE
We all see dates and times from different angles
The GlobalTimeline
1 2 3 4 5 6 7 8 9
Point in absolute time
Coordinated UniversalTime (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
• Has no relationship to geography
LocalTime
• 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
• The point in absolute time
• Whether this time is before or after
another point in time
What we don’t know
• Whether it is morning or night
• What day of the week it is
LocalTime:
Saturday,April 9, 2016 9:11 AM
We Know
• It is Saturday
• It is morning
We Don’t Know
• What point this is on the global
timeline
• Whether it is before or after a time
from a different locality
• What the time will be if we add one
hour to this time
var a = new Date(Date.UTC(2016,0,1));
a.setUTCDate(3);
a.toISOString(); //if you want local, must use different function
//"2016-01-03T00:00:00Z"
moment.utc(‘2016-01-01’).date(3).format();
//"2016-01-03T00:00:00Z"
BROKEN 2:TIME ZONES
There’s no support in Date. Does it matter?
Time Zone Basics
• A time zone is a region that observes a uniform standard time
• A time zone is defined by a set of offsets from UTC
• Offsets are typically in one hour intervals, but not always
• Nepal StandardTime is UTC +5:45
• Time zones frequently have multiple offsets
• There are two main resources for time zones
• Internet Assigned Numbers Authority (IANA)
• WindowsTime Zones
• 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
• In 2016 Egypt decided to do DST 3 months ahead of time, and cancelled it 2 weeks
ahead of time
• The actual time on computers was erratic
• Morocco has 4 transitions due to Ramadan
• Assume nothing
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
Time Zones (With MomentTimeZone)
moment.tz('2016-04-25T02:30:00Z', 'America/Los_Angeles').format()
"2016-04-24T19:30:00-07:00"
moment.tz('2016-04-25T02:30:00Z', 'Europe/Berlin').format()
"2016-04-25T04:30:00+02:00"
BROKEN 3: PARSING
It’s really, really, really broken
Parsing
• JavaScript date will not reliably parse any format
• It tries to reliably parse ISO8601 ("2016-01-03T00:00:00.000Z”) but fails
• The rules for ISO8601 are completely unintuitive
• “When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-
time forms are interpreted as a local time.”
var a = new Date('2016-01-04');
a.toString();
" Sun Jan 03 2016 16:00:00 GMT-0800 (PST) "
var a = new Date('1/4/2016');
a.toString();
" Mon Jan 04 2016 00:00:00 GMT-0800 (PST) "
var a = new Date('2016-01-04T00:00:00');
a.toString();
" Sun Jan 03 2016 16:00:00 GMT-0800 (PST)” //Chrome
“Mon Jan 04 2016 00:00:00 GMT-0800 (PST)” //Firefox
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('February 25, 2016', 'MMMM DD, YYYY').format()
"2016-02-25T00:00:00-06:00"
moment('octubre 25, 2016', 'MMMM DD, YYYY', 'es').format()
"2016-10-25T00:00:00-05:00"
BROKEN 4:
FORMATTING
Your UX person just CAN’T DEAL
Formatting
• JavaScript Date offers very limited formatting options
• .toString
• .toDateString
• .toLocaleString
• .toLocaleDateString
• .toLocaleTimeString
• .toLocaleFormat
• .toISOString
moment().format('MM/DD/YYYY')
"04/26/2016"
moment().format('MMMM D, YYYY hh:mm a')
"April 26, 2016 09:26 pm"
moment().format('YYYY-MM')
“2016-04”
moment().format('LLL')
"April 26, 2016 9:28 PM"
moment().format('L')
"04/26/2016"
moment().locale('en-gb').format('L')
"26/04/2016"
moment().locale('ar').format('LLL')
"٢٦‫أبريل‬ ‫نيسان‬٢٠١٦٢١:٣١"
BROKEN 5: MATH
There are bad APIs, and then there are BAD APIs
Math
• JavaScript Date APIs for Math are very bare-bones
• This frequently causes unintuitive and wrong code
• Moment increases your ability to write readable code
As Seen on Stack Overflow
var startHours = 8;
var startMinutes = 30;
var ed = new Date();
var endHours = ed.getHours();
var endMinutes = ed.getMinutes();
var elapsedMinutes = (endHours * 60 + endMinutes) - (startHours * 60 +
startMinutes);
console.log(elapsedMinutes);
moment().diff(moment(’08:30’, ’HH:mm’), ’minutes');
As Seen on Stack Overflow:With Moment
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');
moment('2016-03-12 12:00').add(1, 'day').format('LLL')
"March 13, 2016 12:00 PM"
moment('2016-03-12 12:00').add(24,'hour').format('LLL')
"March 13, 2016 1:00 PM"
moment('2016-02-28').add(365, 'days').format('LLL')
"February 27, 2017 12:00 AM"
moment('2016-02-28').add(1, 'year').format('LLL')
"February 28, 2017 12:00 AM"
Assuming there were 365 days in a year caused every 30GB Zune to break on December 31, 2008
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
THE FUTURE
We don’t have a DeLorean
Roadmap
• V3
• New Build Process
• Immutability
• V4
• Modularity
• Internal refactor of time zoneAPIs
TC39 Collaboration
• Working with BrianTerlson to create a new DateTime API in JavaScript
• Favoring basing API on the JodaTime family of Date APIs
• Hope to port Moment to live on top of the new API when it is available
Find Us!
• www.momentjs.com
• @momentjs
• @timtamiam –Tim Wood (Author)
• @maggiepint – Maggie Pint
• @mj1856 – Matt Johnson
• @icambron – Isaac Cambron
• Iskren Chernev
• Lucas Sanders

Contenu connexe

Similaire à MomentJS at SeattleJS

How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3allanh0526
 
Comprehensive View on Date-time APIs of Apache Spark 3.0
Comprehensive View on Date-time APIs of Apache Spark 3.0Comprehensive View on Date-time APIs of Apache Spark 3.0
Comprehensive View on Date-time APIs of Apache Spark 3.0Databricks
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4Kenji HASUNUMA
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4Kenji HASUNUMA
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8Serhii Kartashov
 
Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3Kenji HASUNUMA
 
Lesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptxLesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptxLagamaPasala
 
Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3Kenji HASUNUMA
 
Zombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the UndeadZombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the UndeadStephen Chin
 

Similaire à MomentJS at SeattleJS (12)

How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
 
Comprehensive View on Date-time APIs of Apache Spark 3.0
Comprehensive View on Date-time APIs of Apache Spark 3.0Comprehensive View on Date-time APIs of Apache Spark 3.0
Comprehensive View on Date-time APIs of Apache Spark 3.0
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
Java 8 date & time api
Java 8 date & time apiJava 8 date & time api
Java 8 date & time api
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
 
Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3
 
Lesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptxLesson 05 - Time in Distrributed System.pptx
Lesson 05 - Time in Distrributed System.pptx
 
Fun times with ruby
Fun times with rubyFun times with ruby
Fun times with ruby
 
Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3
 
Zombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the UndeadZombie Time - JSR 310 for the Undead
Zombie Time - JSR 310 for the Undead
 
Timezone Mess
Timezone MessTimezone Mess
Timezone Mess
 

Plus de Maggie Pint

Programming in the 4th Dimension
Programming in the 4th DimensionProgramming in the 4th Dimension
Programming in the 4th DimensionMaggie Pint
 
Maintaining maintainers(copy)
Maintaining maintainers(copy)Maintaining maintainers(copy)
Maintaining maintainers(copy)Maggie Pint
 
Got documents Code Mash Revision
Got documents Code Mash RevisionGot documents Code Mash Revision
Got documents Code Mash RevisionMaggie Pint
 
It Depends - Database admin for developers - Rev 20151205
It Depends - Database admin for developers - Rev 20151205It Depends - Database admin for developers - Rev 20151205
It Depends - Database admin for developers - Rev 20151205Maggie Pint
 
Got documents - The Raven Bouns Edition
Got documents - The Raven Bouns EditionGot documents - The Raven Bouns Edition
Got documents - The Raven Bouns EditionMaggie Pint
 

Plus de Maggie Pint (7)

Programming in the 4th Dimension
Programming in the 4th DimensionProgramming in the 4th Dimension
Programming in the 4th Dimension
 
Maintaining maintainers(copy)
Maintaining maintainers(copy)Maintaining maintainers(copy)
Maintaining maintainers(copy)
 
Got documents Code Mash Revision
Got documents Code Mash RevisionGot documents Code Mash Revision
Got documents Code Mash Revision
 
It Depends - Database admin for developers - Rev 20151205
It Depends - Database admin for developers - Rev 20151205It Depends - Database admin for developers - Rev 20151205
It Depends - Database admin for developers - Rev 20151205
 
Got documents - The Raven Bouns Edition
Got documents - The Raven Bouns EditionGot documents - The Raven Bouns Edition
Got documents - The Raven Bouns Edition
 
Got documents?
Got documents?Got documents?
Got documents?
 
It Depends
It DependsIt Depends
It Depends
 

Dernier

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Dernier (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

MomentJS at SeattleJS

  • 1.
  • 3. var a = new Date('2016-01-04'); a.toString(); " Sun Jan 03 2016 16:00:00 GMT-0800 (PST) " var a = new Date('1/4/2016'); a.toString(); " Mon Jan 04 2016 00:00:00 GMT-0800 (PST) "
  • 4. Brokens! 1. UTC and Local Context Switches 2. NoTime Zone Support 3. Parsing (all of it) • The spec is a disaster 4. Formatting 5. Math APIs
  • 6. Facts • Primary Date andTime Library for JavaScript • Community Based • 5Years Old • 5 Core Maintainers and 1 Author Emeritus • All part-time, unpaid • No Corporate backing • 28,157 Stars • 3,961 Forks • 6,676,292 NPM Downloads In the Last Month • 10th Most Depended Upon NPM Package
  • 7. FIXING DATE 10 Days for aWhole Language
  • 8. BROKEN 1: UTC AND LOCAL
  • 10. PERSPECTIVE We all see dates and times from different angles
  • 11. The GlobalTimeline 1 2 3 4 5 6 7 8 9 Point in absolute time
  • 12. Coordinated UniversalTime (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 • Has no relationship to geography
  • 13. LocalTime • 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)
  • 14. UTC Time: 2016-04-09T14:17:47Z What we know • The point in absolute time • Whether this time is before or after another point in time What we don’t know • Whether it is morning or night • What day of the week it is
  • 15. LocalTime: Saturday,April 9, 2016 9:11 AM We Know • It is Saturday • It is morning We Don’t Know • What point this is on the global timeline • Whether it is before or after a time from a different locality • What the time will be if we add one hour to this time
  • 16. var a = new Date(Date.UTC(2016,0,1)); a.setUTCDate(3); a.toISOString(); //if you want local, must use different function //"2016-01-03T00:00:00Z" moment.utc(‘2016-01-01’).date(3).format(); //"2016-01-03T00:00:00Z"
  • 17. BROKEN 2:TIME ZONES There’s no support in Date. Does it matter?
  • 18. Time Zone Basics • A time zone is a region that observes a uniform standard time • A time zone is defined by a set of offsets from UTC • Offsets are typically in one hour intervals, but not always • Nepal StandardTime is UTC +5:45 • Time zones frequently have multiple offsets • There are two main resources for time zones • Internet Assigned Numbers Authority (IANA) • WindowsTime Zones • IANA time zones are more widely used, and more accurate
  • 20. Politics • Time zones are inherently political • Governments change time zones regularly • Governments change time zones suddenly • In 2016 Egypt decided to do DST 3 months ahead of time, and cancelled it 2 weeks ahead of time • The actual time on computers was erratic • Morocco has 4 transitions due to Ramadan • Assume nothing
  • 21. 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
  • 22. Time Zones (With MomentTimeZone) moment.tz('2016-04-25T02:30:00Z', 'America/Los_Angeles').format() "2016-04-24T19:30:00-07:00" moment.tz('2016-04-25T02:30:00Z', 'Europe/Berlin').format() "2016-04-25T04:30:00+02:00"
  • 23. BROKEN 3: PARSING It’s really, really, really broken
  • 24. Parsing • JavaScript date will not reliably parse any format • It tries to reliably parse ISO8601 ("2016-01-03T00:00:00.000Z”) but fails • The rules for ISO8601 are completely unintuitive • “When the time zone offset is absent, date-only forms are interpreted as a UTC time and date- time forms are interpreted as a local time.”
  • 25. var a = new Date('2016-01-04'); a.toString(); " Sun Jan 03 2016 16:00:00 GMT-0800 (PST) " var a = new Date('1/4/2016'); a.toString(); " Mon Jan 04 2016 00:00:00 GMT-0800 (PST) " var a = new Date('2016-01-04T00:00:00'); a.toString(); " Sun Jan 03 2016 16:00:00 GMT-0800 (PST)” //Chrome “Mon Jan 04 2016 00:00:00 GMT-0800 (PST)” //Firefox
  • 26. 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('February 25, 2016', 'MMMM DD, YYYY').format() "2016-02-25T00:00:00-06:00" moment('octubre 25, 2016', 'MMMM DD, YYYY', 'es').format() "2016-10-25T00:00:00-05:00"
  • 27. BROKEN 4: FORMATTING Your UX person just CAN’T DEAL
  • 28. Formatting • JavaScript Date offers very limited formatting options • .toString • .toDateString • .toLocaleString • .toLocaleDateString • .toLocaleTimeString • .toLocaleFormat • .toISOString
  • 29. moment().format('MM/DD/YYYY') "04/26/2016" moment().format('MMMM D, YYYY hh:mm a') "April 26, 2016 09:26 pm" moment().format('YYYY-MM') “2016-04”
  • 30. moment().format('LLL') "April 26, 2016 9:28 PM" moment().format('L') "04/26/2016" moment().locale('en-gb').format('L') "26/04/2016" moment().locale('ar').format('LLL') "٢٦‫أبريل‬ ‫نيسان‬٢٠١٦٢١:٣١"
  • 31. BROKEN 5: MATH There are bad APIs, and then there are BAD APIs
  • 32. Math • JavaScript Date APIs for Math are very bare-bones • This frequently causes unintuitive and wrong code • Moment increases your ability to write readable code
  • 33. As Seen on Stack Overflow var startHours = 8; var startMinutes = 30; var ed = new Date(); var endHours = ed.getHours(); var endMinutes = ed.getMinutes(); var elapsedMinutes = (endHours * 60 + endMinutes) - (startHours * 60 + startMinutes); console.log(elapsedMinutes);
  • 35. var a = new Date(); a.setDate(a.getDate() - 5); moment().subtract(5, 'days');
  • 36. 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')
  • 37. 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');
  • 38.
  • 39. moment('2016-03-12 12:00').add(1, 'day').format('LLL') "March 13, 2016 12:00 PM" moment('2016-03-12 12:00').add(24,'hour').format('LLL') "March 13, 2016 1:00 PM"
  • 40. moment('2016-02-28').add(365, 'days').format('LLL') "February 27, 2017 12:00 AM" moment('2016-02-28').add(1, 'year').format('LLL') "February 28, 2017 12:00 AM" Assuming there were 365 days in a year caused every 30GB Zune to break on December 31, 2008
  • 41. 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"
  • 42. 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
  • 43. THE FUTURE We don’t have a DeLorean
  • 44. Roadmap • V3 • New Build Process • Immutability • V4 • Modularity • Internal refactor of time zoneAPIs
  • 45. TC39 Collaboration • Working with BrianTerlson to create a new DateTime API in JavaScript • Favoring basing API on the JodaTime family of Date APIs • Hope to port Moment to live on top of the new API when it is available
  • 46. Find Us! • www.momentjs.com • @momentjs • @timtamiam –Tim Wood (Author) • @maggiepint – Maggie Pint • @mj1856 – Matt Johnson • @icambron – Isaac Cambron • Iskren Chernev • Lucas Sanders