● videoExtraction
Tangent: Working with Dates

Unlock PostgreSQL Fundamentals
2 hours of SQL video using NASA's Cassini data. See why developers become data pros.
SECTION
Extraction
NEXT UP
Validating the Master Plan
COURSE
PostgreSQL Fundamentals
28 lessons
About this lesson
Bottom line: never trust a spreadsheet. You're going to hear me say that a lot in this production! Especially when it comes to dates.
Postgres is pretty good at dealing with dates... in fact it's amazingly powerful as well as correct:
<span class="hljs-keyword">select</span> now(); <span class="hljs-comment">-- what date and time is it where my server is located?</span>
<span class="hljs-keyword">select</span> now() <span class="hljs-operator">+</span> <span class="hljs-string">'1 day'</span> <span class="hljs-keyword">as</span> tomorrow; <span class="hljs-comment">-- adding an interval is extremely easy</span>
<span class="hljs-keyword">select</span> now() <span class="hljs-keyword">at</span> <span class="hljs-type">time</span> zone <span class="hljs-string">'America/New_York'</span>; <span class="hljs-comment">-- specifying a timezone</span>
If you're reading this in a browser, which I assume you are, open up the developer tools using CMD-shift-i (or Ctrl-shift-i on Windows) and open the console.
To see a typical date for JavaScript (and many other languages):
<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>() <span class="hljs-comment">//prints out a long-form date</span>
To see an ISO date, which most databases like, you can use:
<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>().<span class="hljs-title function_">toISOString</span>();
This is a format you should let your eyes get used to.
Unlock PostgreSQL Fundamentals
2 hours of SQL video using NASA's Cassini data. See why developers become data pros.