Timestamp in JavaScript

Little tutorial with code examples and video on timestamp in JavaScript
11 September 2017   2903

In order to get a timestamp in JavaScript, use this code. 

var timeStampInMs = window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now();

console.log(timeStampInMs, Date.now());

In short:

+ new Date()

A unary operator like plus triggers the valueOf method in the Date object and it returns the timestamp (without any alteration).

Details:

On almost all current browsers you can use Date.now() to get the UTC timestamp in milliseconds; a notable exception to this is IE8 and earlier.

You can easily make a shim for this, though:

if (!Date.now) {
    Date.now = function() { return new Date().getTime(); }
}

To get the timestamp in seconds, you can use:

Math.floor(Date.now() / 1000)

Or alternatively you could use:

Date.now() / 1000 | 0

Which should be slightly faster, but also less readable.

Note that using Date.now() is slightly better because it's shorter & doesn't create a new Date object. However, if you don't want a shim & maximum compatibility, you could use the "old" method to get the timestamp in milliseconds:

new Date().getTime()

Which you can then convert to seconds like this:

Math.round(new Date().getTime()/1000)

And you can also use the valueOf method which we showed above:

new Date().valueOf()

Frontend News Digest 3 -9.08

Learn this week's latest news and updates of JavaScript, CSS, PHP and related technologies
08 August 2019   626

Greetings! I hope your week went great! Here's new Frontend news digest.

In this digest, you will learn how to improve your procedural code, how to deploy apps to to IBM cloud with Tekton, how to use Immer to manage your React state and much more!

Guides

  • Trigger Firebase Storage Image Resize with Cloud Functions

Tutorial on creating an imager resizer triggered by Firebase Storage

  • Using pipe and compose to improve procedural code

According to the creator, this guide will sohw you how to use pipe and compose to improve the way we write procedural code.

  • Deploying Applications to IBM Cloud Kubernetes via Tekton

This article talks about Tekton, an open-source framework for creating CI/CD systems, and explains how to deploy and use it on the IBM Cloud Kubernetes service

  • Quick Gulp Cache Busting

This tips will allow you to improve your work with Gulp

  • Using Immer for React State Management

Learn how Immer can help you to operate React states

Article

  • Laravel Cloud Source Code on GitHub

Author believes that is a chance to get a glimpse of an application written by the creator of Laravel

Updates

  • Calendario

Library allowing to create flexible calendars

  • Symphony

According to the creators, 113 pull requests were merged (79 in code and 34 in docs) and 55 issues were closed (43 in code and 12 in docs)

Video

  • The Date Object - Getting, Setting & Formatting Dates in JavaScript - Tutorial

  • Simple Slide Show in JavaScript | JavaScript exercises