PHP's idate in JavaScript

Here’s what our current JavaScript equivalent to PHP's idate looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module.exports = function idate (format, timestamp) {
// discuss at: http://locutus.io/php/idate/
// original by: Brett Zamir (http://brett-zamir.me)
// original by: date
// original by: gettimeofday
// input by: Alex
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// improved by: Theriault (https://github.com/Theriault)
// example 1: idate('y', 1255633200)
// returns 1: 9
if (format === undefined) {
throw new Error('idate() expects at least 1 parameter, 0 given')
}
if (!format.length || format.length > 1) {
throw new Error('idate format is one char')
}
// @todo: Need to allow date_default_timezone_set() (check for $locutus.default_timezone and use)
var _date = (typeof timestamp === 'undefined')
? new Date()
: (timestamp instanceof Date)
? new Date(timestamp)
: new Date(timestamp * 1000)
var a
switch (format) {
case 'B':
return Math.floor((
(_date.getUTCHours() * 36e2) +
(_date.getUTCMinutes() * 60) +
_date.getUTCSeconds() + 36e2
) / 86.4) % 1e3
case 'd':
return _date.getDate()
case 'h':
return _date.getHours() % 12 || 12
case 'H':
return _date.getHours()
case 'i':
return _date.getMinutes()
case 'I':
// capital 'i'
// Logic original by getimeofday().
// Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
// If they are not equal, then DST is observed.
a = _date.getFullYear()
return 0 + (((new Date(a, 0)) - Date.UTC(a, 0)) !== ((new Date(a, 6)) - Date.UTC(a, 6)))
case 'L':
a = _date.getFullYear()
return (!(a & 3) && (a % 1e2 || !(a % 4e2))) ? 1 : 0
case 'm':
return _date.getMonth() + 1
case 's':
return _date.getSeconds()
case 't':
return (new Date(_date.getFullYear(), _date.getMonth() + 1, 0))
.getDate()
case 'U':
return Math.round(_date.getTime() / 1000)
case 'w':
return _date.getDay()
case 'W':
a = new Date(
_date.getFullYear(),
_date.getMonth(),
_date.getDate() - (_date.getDay() || 7) + 3
)
return 1 + Math.round((a - (new Date(a.getFullYear(), 0, 4))) / 864e5 / 7)
case 'y':
return parseInt((_date.getFullYear() + '')
.slice(2), 10) // This function returns an integer, unlike _date()
case 'Y':
return _date.getFullYear()
case 'z':
return Math.floor((_date - new Date(_date.getFullYear(), 0, 1)) / 864e5)
case 'Z':
return -_date.getTimezoneOffset() * 60
default:
throw new Error('Unrecognized _date format token')
}
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/datetime/idate'). You could also require the datetime module in full so that you could access datetime.idate instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1idate('y', 1255633200)9

« More PHP datetime functions