• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

How to Work With DATETIME Values in JavaScript

Javascript · October 7, 2021

Dates in Javascript have always been a PITA. It is one of the reasons that libraries like MomentJS reigned supreme for so long. Things are complicated, especially if you’re dealing with different timezones. Fortunately, working with simple DATETIME values has never really been a problem.

What prompted this post was that I am working with WordPress a lot at the moment, and all dates in WordPress are MySQL DATETIME formatted dates. Say you have a DATETIME value that looks like this: 2021-10-05 00:00:00 — you can use the Javascript Date object on this because it’s a valid date value.

In your developer tools in the browser, you can pass this value into Date and it will spit out something that looks like the below:

For brevity, here is the code itself:

new Date('2021-10-05 00:00:00');

Get the UNIX timestamp

So, you have your date object. You can now easily convert the value to a UNIX timestamp using getTime(). This allows you to do things like sort elements by integers instead of date objects. You can do safe comparative checks and use the .sort method.

Here is the code:

Math.floor(new Date('2021-10-05 00:00:00').getTime() / 1000);

We have to divide this value by 1000 because the Javascript getTime() will return milliseconds since the UNIX epoch. Unix time is in seconds, so dividing the milliseconds gives us the seconds representation.

The reason we use Math.floor here is quite important too. Some examples online will use Math.round which is not accurate. We want the actual elapsed seconds and using floor will not give us that. Because dividing by 1000 can sometimes result in fractional numbers, we use this to get a nice clean value.

Chances are, if you’re reading this, you’re working with dates that are coming from a database like MySQL. Luckily, Javascript makes working with these dates relatively painless. Just don’t mention timezones. That conversation is for another post entirely.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • I Joined Truth Social Using a VPN and Editing Some HTML to Bypass the Phone Verification
  • How To Install Eufy Security Cameras Without Drilling or Using Screws
  • How To Get The Hash of A File In Node.js
  • Wild Natural Deodorant Review
  • The Most Common iPhone Passcodes (and how to guess them)
  • How to Record With the Neural DSP Quad Cortex in Reaper (DI and USB Recording)
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • How To Paginate An Array In Javascript
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?

Recent Comments

  • CJ on Microsoft Modern Wireless Headset Review
  • Dwayne on Microsoft Modern Wireless Headset Review
  • CJ on Microsoft Modern Wireless Headset Review
  • john on Microsoft Modern Wireless Headset Review
  • Dwayne on Why You Should Be Using globalThis Instead of Window In Your Javascript Code

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz