Programming Languages - Date / Time
Last Updated: 2024-01-21
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
Conversion Between Unix Time and Readable Timestamp
Convert from seconds to time string:
$ date -d @1622999905
Sun Jun 6 10:18:25 PDT 202
In ISO 8601 format:
$ date -d @1622999905 -Iseconds
2021-06-06T17:18:25+00:00
With timezone:
$ TZ=":America/Los_Angeles" date -d @1622999905
Sun 06 Jun 2021 10:18:25 AM PDT
Get Running Time
Java
long start = System.currentTimeMillis();
method();
long end = System.currentTimeMillis();
System.out.println("Time used: " + (end – start));
Create Padded Dates
$ seq -f "%02g" 1 31
01
02
03
04
05
...