Last active
May 25, 2018 02:15
-
-
Save NTICompass/9375143 to your computer and use it in GitHub Desktop.
Use PHP's date() formats in moment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(m){ | |
/* | |
* PHP => moment.js | |
* | |
* http://www.php.net/manual/en/function.date.php | |
* http://momentjs.com/docs/#/displaying/format/ | |
*/ | |
var formatMap = { | |
d: 'DD', | |
D: 'ddd', | |
j: 'D', | |
l: 'dddd', | |
N: 'E', | |
S: function(){ | |
return '['+this.format('Do').replace(/\d*/g, '')+']'; | |
}, | |
w: 'd', | |
z: function(){ | |
return this.format('DDD') - 1; | |
}, | |
W: 'W', | |
F: 'MMMM', | |
m: 'MM', | |
M: 'MMM', | |
n: 'M', | |
t: function(){ | |
return this.daysInMonth(); | |
}, | |
L: function(){ | |
return this.isLeapYear() ? 1 : 0; | |
}, | |
o: 'GGGG', | |
Y: 'YYYY', | |
y: 'YY', | |
a: 'a', | |
A: 'A', | |
B: function(){ | |
var thisUTC = this.clone().utc(), | |
// Shamelessly stolen from http://javascript.about.com/library/blswatch.htm | |
swatch = ((thisUTC.hours()+1) % 24) + (thisUTC.minutes() / 60) + (thisUTC.seconds() / 3600); | |
return Math.floor(swatch * 1000 / 24); | |
}, | |
g: 'h', | |
G: 'H', | |
h: 'hh', | |
H: 'HH', | |
i: 'mm', | |
s: 'ss', | |
u: '[u]', // not sure if moment has this | |
e: '[e]', // moment does not have this | |
I: function(){ | |
return this.isDST() ? 1 : 0; | |
}, | |
O: 'ZZ', | |
P: 'Z', | |
T: '[T]', // deprecated in moment | |
Z: function(){ | |
return parseInt(this.format('ZZ'), 10) * 36; | |
}, | |
c: 'YYYY-MM-DD[T]HH:mm:ssZ', | |
r: 'ddd, DD MMM YYYY HH:mm:ss ZZ', | |
U: 'X' | |
}, | |
formatEx = /[dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU]/g; | |
m.fn.formatPHP = function(format){ | |
var that = this; | |
return this.format(format.replace(formatEx, function(phpStr){ | |
return typeof formatMap[phpStr] === 'function' ? formatMap[phpStr].call(that) : formatMap[phpStr]; | |
})); | |
}; | |
}(moment)); |
I have a problem. If I use this line of code:
moment.fn.formatPHP("d.m.Y h:i");
then a got the following error:
TypeError: m._d is undefined (moment-with-locale.js line 79)
I use the latest moment.js 2.10.2. Am I doing something wrong or is it an error of this script?
In case anyone stumbles upon this you have to make some changes to get it to work. i went ahead and made a new Gist to make it easier.
Located Here
https://gist.github.com/phpmypython/f97c5f5f59f2a934599d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this :-)