Skip to content

Instantly share code, notes, and snippets.

@kubiqsk
Created July 25, 2022 07:20
Show Gist options
  • Save kubiqsk/c60207a3075104df7cc1822a95053ecd to your computer and use it in GitHub Desktop.
Save kubiqsk/c60207a3075104df7cc1822a95053ecd to your computer and use it in GitHub Desktop.
PHP date() formatting in JavaScript
/*
# this will create new .format() function for Date object
# there is auto localization from Intl, but you can also set language as a 2nd argument
# all possible values were tested and compared on Apache server with PHP 8 date() function
# you can find minified version in the first comment
# examples:
new Date().format('d.m.Y H:i:s') // 25.07.2022 09:11
new Date().format( 'l, j. F Y', 'de' ) // Montag, 25. Juli 2022
*/
(function(){
var replaceChars = {
// day
d: function(){ return ( '0' + this.getDate() ).slice(-2) },
D: function( locale ){ return new Intl.DateTimeFormat( locale, { weekday: 'short' } ).format( this ) },
j: function(){ return this.getDate() },
l: function( locale ){ return new Intl.DateTimeFormat( locale, { weekday: 'long' } ).format( this ) },
N: function(){
let day = this.getDay();
return day === 0 ? 7 : day;
},
S: function(){
let date = this.getDate();
return date % 10 === 1 && date !== 11 ? 'st' : ( date % 10 === 2 && date !== 12 ? 'nd' : ( date % 10 === 3 && date !== 13 ? 'rd' : 'th' ) );
},
w: function(){ return this.getDay() },
z: function(){ return Math.floor( ( this - new Date( this.getFullYear(), 0, 1 ) ) / 86400000 ) },
// week
W: function(){
let target = new Date( this.valueOf() );
let dayNr = ( this.getDay() + 6 ) % 7;
target.setDate( target.getDate() - dayNr + 3 );
let firstThursday = target.valueOf();
target.setMonth( 0, 1 );
if( target.getDay() !== 4 ){
target.setMonth( 0, 1 + ( ( 4 - target.getDay() ) + 7 ) % 7 );
}
return Math.ceil( ( firstThursday - target ) / 604800000 ) + 1;
},
// month
F: function( locale ){ return new Intl.DateTimeFormat( locale, { month: 'long' } ).format( this ) },
m: function(){ return ( '0' + ( this.getMonth() + 1 ) ).slice(-2) },
M: function( locale ){ return new Intl.DateTimeFormat( locale, { month: 'short' } ).format( this ) },
n: function(){ return this.getMonth() + 1 },
t: function(){
let year = this.getFullYear();
let nextMonth = this.getMonth() + 1;
if( nextMonth === 12 ){
year = year++;
nextMonth = 0;
}
return new Date( year, nextMonth, 0 ).getDate();
},
// year
L: function(){
let year = this.getFullYear();
return year % 400 === 0 || ( year % 100 !== 0 && year % 4 === 0 ) ? 1 : 0;
},
o: function(){
let date = new Date( this.valueOf() );
date.setDate( date.getDate() - ( ( this.getDay() + 6 ) % 7 ) + 3 );
return date.getFullYear();
},
Y: function(){ return this.getFullYear() },
y: function(){ return ( '' + this.getFullYear() ).slice(-2) },
// time
a: function(){ return this.getHours() < 12 ? 'am' : 'pm' },
A: function(){ return this.getHours() < 12 ? 'AM' : 'PM' },
B: function(){
return ( '00' + Math.floor( ( ( ( this.getUTCHours() + 1 ) % 24 ) + this.getUTCMinutes() / 60 + this.getUTCSeconds() / 3600 ) * 1000 / 24 ) ).slice(-3);
},
g: function(){ return this.getHours() % 12 || 12 },
G: function(){ return this.getHours() },
h: function(){ return ( '0' + ( this.getHours() % 12 || 12 ) ).slice(-2) },
H: function(){ return ( '0' + this.getHours() ).slice(-2) },
i: function(){ return ( '0' + this.getMinutes() ).slice(-2) },
s: function(){ return ( '0' + this.getSeconds() ).slice(-2) },
v: function(){ return ( '00' + this.getMilliseconds() ).slice(-3) },
// Timezone
e: function(){ return Intl.DateTimeFormat().resolvedOptions().timeZone },
I: function(){
let DST = null;
for( let i = 0; i < 12; ++i ){
let d = new Date( this.getFullYear(), i, 1 );
let offset = d.getTimezoneOffset();
if( DST === null ){
DST = offset;
}else if( offset < DST ){
DST = offset;
break;
}else if( offset > DST ){
break;
}
}
return ( this.getTimezoneOffset() === DST ) | 0;
},
O: function(){
let timezoneOffset = this.getTimezoneOffset();
return ( -timezoneOffset < 0 ? '-' : '+' ) + ( '0' + Math.floor( Math.abs( timezoneOffset / 60 ) ) ).slice(-2) + ( '0' + Math.abs( timezoneOffset % 60 ) ).slice(-2);
},
P: function(){
let timezoneOffset = this.getTimezoneOffset();
return ( -timezoneOffset < 0 ? '-' : '+' ) + ( '0' + Math.floor( Math.abs( timezoneOffset / 60 ) ) ).slice(-2) + ':' + ( '0' + Math.abs( timezoneOffset % 60 ) ).slice(-2);
},
T: function( locale ){
let timeString = this.toLocaleTimeString( locale, { timeZoneName: 'short' } ).split(' ');
let abbr = timeString[ timeString.length - 1 ];
return abbr == 'GMT+1' ? 'CET' : ( abbr == 'GMT+2' ? 'CEST' : abbr );
},
Z: function(){ return -this.getTimezoneOffset() * 60 },
// Full Date/Time
c: function(){ return this.format('Y-m-d\\TH:i:sP') },
r: function(){ return this.format('D, d M Y H:i:s O') },
U: function(){ return Math.floor( this.getTime() / 1000 ) }
}
Date.prototype.format = function( formatStr, locale = navigator.language ){
var date = this;
return formatStr.replace( /(\\?)(.)/g, function( _, esc, chr ){
return esc === '' && replaceChars[ chr ] ? replaceChars[ chr ].call( date, locale ) : chr
})
}
}).call( this );
@kubiqsk
Copy link
Author

kubiqsk commented Jul 25, 2022

minified version

!function(){var u={d:function(){return("0"+this.getDate()).slice(-2)},D:function(t){return new Intl.DateTimeFormat(t,{weekday:"short"}).format(this)},j:function(){return this.getDate()},l:function(t){return new Intl.DateTimeFormat(t,{weekday:"long"}).format(this)},N:function(){var t=this.getDay();return 0===t?7:t},S:function(){var t=this.getDate();return t%10==1&&11!==t?"st":t%10==2&&12!==t?"nd":t%10==3&&13!==t?"rd":"th"},w:function(){return this.getDay()},z:function(){return Math.floor((this-new Date(this.getFullYear(),0,1))/864e5)},W:function(){let t=new Date(this.valueOf());var e=(this.getDay()+6)%7;t.setDate(t.getDate()-e+3);e=t.valueOf();return t.setMonth(0,1),4!==t.getDay()&&t.setMonth(0,1+(4-t.getDay()+7)%7),Math.ceil((e-t)/6048e5)+1},F:function(t){return new Intl.DateTimeFormat(t,{month:"long"}).format(this)},m:function(){return("0"+(this.getMonth()+1)).slice(-2)},M:function(t){return new Intl.DateTimeFormat(t,{month:"short"}).format(this)},n:function(){return this.getMonth()+1},t:function(){let t=this.getFullYear(),e=this.getMonth()+1;return 12===e&&(t=t++,e=0),new Date(t,e,0).getDate()},L:function(){var t=this.getFullYear();return t%400==0||t%100!=0&&t%4==0?1:0},o:function(){let t=new Date(this.valueOf());return t.setDate(t.getDate()-(this.getDay()+6)%7+3),t.getFullYear()},Y:function(){return this.getFullYear()},y:function(){return(""+this.getFullYear()).slice(-2)},a:function(){return this.getHours()<12?"am":"pm"},A:function(){return this.getHours()<12?"AM":"PM"},B:function(){return("00"+Math.floor(1e3*((this.getUTCHours()+1)%24+this.getUTCMinutes()/60+this.getUTCSeconds()/3600)/24)).slice(-3)},g:function(){return this.getHours()%12||12},G:function(){return this.getHours()},h:function(){return("0"+(this.getHours()%12||12)).slice(-2)},H:function(){return("0"+this.getHours()).slice(-2)},i:function(){return("0"+this.getMinutes()).slice(-2)},s:function(){return("0"+this.getSeconds()).slice(-2)},v:function(){return("00"+this.getMilliseconds()).slice(-3)},e:function(){return Intl.DateTimeFormat().resolvedOptions().timeZone},I:function(){let n=null;for(let e=0;e<12;++e){let t=new Date(this.getFullYear(),e,1);var r=t.getTimezoneOffset();if(null===n)n=r;else{if(r<n){n=r;break}if(r>n)break}}return this.getTimezoneOffset()===n|0},O:function(){var t=this.getTimezoneOffset();return(-t<0?"-":"+")+("0"+Math.floor(Math.abs(t/60))).slice(-2)+("0"+Math.abs(t%60)).slice(-2)},P:function(){var t=this.getTimezoneOffset();return(-t<0?"-":"+")+("0"+Math.floor(Math.abs(t/60))).slice(-2)+":"+("0"+Math.abs(t%60)).slice(-2)},T:function(t){t=this.toLocaleTimeString(t,{timeZoneName:"short"}).split(" "),t=t[t.length-1];return"GMT+1"==t?"CET":"GMT+2"==t?"CEST":t},Z:function(){return 60*-this.getTimezoneOffset()},c:function(){return this.format("Y-m-d\\TH:i:sP")},r:function(){return this.format("D, d M Y H:i:s O")},U:function(){return Math.floor(this.getTime()/1e3)}};Date.prototype.format=function(t,r=navigator.language){var i=this;return t.replace(/(\\?)(.)/g,function(t,e,n){return""===e&&u[n]?u[n].call(i,r):n})}}.call(this);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment