![]() ![]() To solve this, we need a common reference point and a time standard. If you were to count how long it took for that shadow to move from point to point, you could (kind of) keep track of time.īut there’s a problem… If I put a vertical pole in the ground where I live in the U.S., the shadows aren’t going to be cast in the same direction at the same time as someone putting a pole in the ground somewhere in Ireland. ![]() When the sun rises, that pole is going to cast a shadow west, and conversely, will cast a shadow east as the sun sets. Imagine sticking a perfectly vertical pole in the ground on a perfectly sunny day. If you don’t care to read my brief history lesson, skip ahead to the next part of this post. WARNING: We’re going to get distracted for a few minutes here, but if we really want to remember how these pesky JavaScript dates work, we need some historical context. Well… Kinda.īefore we had a record of time on our wrists, in our pockets, on our walls, and on our overpriced coffee makers, people needed a reliable way to calculate the current time. UTC stands for “Coordinated Universal Time”, and per the name, it is the basis for how time is kept. What is UTC? (the key to understanding JavaScript dates) But with a little history lesson, I think we can get this to a manageable place. While I agree that JavaScript can be unnecessarily confusing sometimes, when it comes to dates, * the entire system is confusing *- especially when we start talking about timezones. But it won’t make sense until we explore the internals of this process more. This, along with some quirks of JavaScript is why we got such unexpected results above. If we’re going to learn this once and for all, we need to start by learning about a computer’s timezone.Īs you can see, the local computer’s timezone is EST/EDT, while the server’s timezone is UTC. This is the last time you will be confused by JavaScript dates Anyways, it’s time to solve yet another JavaScript mystery. I guess we shouldn’t be surprised because JavaScript is a language full of fun surprises. And furthermore, both computers returned the same result for the toISOString() method!Įither I’m going crazy, or we just typed in the exact same thing on two different computers and got a completely different answer - not to mention one of the computers got my date completely wrong!! Or did it… toDateString() was Tue Sep 29 2020, while on my server, the result of. Notice anything? On my local computer, the result of. Here’s my local computer (the computer I’m standing at right now in the Eastern part of the United States):Īnd here is the webserver that I run my golf training application on: Imagine this - you‘ve been coding for 8 hours, your brain is a bit tired, and you console.log one of your dates during an intense debugging session:Įnter fullscreen mode Exit fullscreen mode It seems like all the cool kids are using MomentJS these days (or if you’re even cooler - Luxon).ĭon’t worry, I’m not some JavaScript purist here telling you to always work with vanilla JS Date Objects - I just want to work through the core concepts that will make your life with Moment/Luxon much much easier. Most people search for tutorials on JavaScript dates when all they needed was a review of how time works! (including myself)īefore I start reading… What about MomentJS? As you read this post, you’ll realize that the difficult part of JavaScript dates is the non-JavaScript concepts like timezones and formatting standards. JavaScript dates aren’t actually that difficult. And if I had to guess, a few of you reading this are in a similar spot, so cheers to that! □ It took me at least 3 years of programming before I finally sat down to understand this topic. If your dates don’t handle timezones correctly and you end up with an “off by 1” error, your user who missed their job interview is going to be FURIOUS with your app. ![]() ![]() Furthermore, they might be the least exciting thing to learn about in JavaScript programming.īut what happens if you are building a Todo app - you know, the most overused web application in existence, and you want to add calendar reminder functionality? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |