Polyglot CheatSheet - DateTime

Updated: 2018-06-11

Get Current Time

Javascript

var date = new Date();

// date
date.getDate();

// month(0-11)
date.getMonth() + 1;

// year(201x)
date.getFullYear();

// get previous day
date.setDate(date.getDate() - 1);

// get epoch
new Date() - new Date(0);

Bash

Suppose today is 2020-01-01

$ date +%Y%m%d
20200101

Format Date/Time

Javascript

No easy way in js, but can use moment.js instead

moment().format('YYYY-MM-DD');

Get Running Time

Java

long start = System.currentTimeMillis();
method();
long end = System.currentTimeMillis();

System.out.println("Time used: " + (end – start));