Calling strftime() without parameters will cause the current date to be displayed using the default format string.
$('#strftime-1').strftime( );
Results:
Testing strftime() using various format strings.
$('#strftime-2').strftime( '%Y-%m-%d %H:%M:%S (%y)' ); $('#strftime-3').strftime( '%A %d %B %Y' ); $('#strftime-4').strftime( '%a %d %b %y' );
Results:
Using '%%' in the format string to output the '%' character.
$('#strftime-5').strftime( '%%' );
Results:
Passing invalid format strings to strftime()
$('#strftime-6').strftime( '%z %w % %' );
Results:
In addition to the format string, strftime() can accept other parameters.
It is possible to pass strftime() a specific date to display using a standard Date object.
var _dt = new Date( "Feb 19, 1981 05:30" ); $('#strftime-7').strftime( '%Y-%m-%d %H:%M:%S' , _dt ); $('#strftime-8').strftime( null , _dt );
Results:
strftime()'s third parameter indicates whether UTC time should be used.
$('#strftime-9').strftime( null , null , true ); $('#strftime-10').strftime( '%Y-%m-%d %H:%M:%S (%y)' , null , true ); $('#strftime-11').strftime( '%Y-%m-%d %H:%M:%S (%y)' , _dt , true );
Results:
While strftime() doesn't support locales explicitely, the strings it uses can be replaced.
Using $.strftime.setText() to change the day-of-week and month strings.
try { $.strftime.setText({ 'days_short' : [ 'Dim', 'Lun' , 'Mar' , 'Mer' , 'Jeu' , 'Ven' , 'Sam' ] , 'days_long' : [ 'Dimanche' , 'Lundi' , 'Mardi' , 'Mercredi' , 'Jeudi' , 'Vendredi' , 'Samedi' ] , 'months_short' : [ 'Jan' , 'Fev' , 'Mar' , 'Avr' , 'Mai' , 'Jui' , 'Jul' , 'Auo' , 'Sep' , 'Oct' , 'Nov' , 'Déc' ] , 'months_long' : [ 'Janvier' , 'Février' , 'Mars' , 'Avril' , 'Mai' , 'Juin' , 'Juillet' , 'Août' , 'Septembre' , 'Octobre' , 'Novembre' , 'Décembre' ] , 'format' : '%m/%d/%Y' }); $('#strftime-settext-1').html( 'setText() : success' ); } catch ( e ) { $('#strftime-settext-1').html( 'setText() : error ' + e.message ); } $('#strftime-settext-2').strftime( ); $('#strftime-settext-3').strftime( '%A %d %B %Y' );
Results:
If incorrect data is passed to $.strftime.setText(), exceptions will be thrown.
try { $.strftime.setText({ }); $('#strftime-settext-4').html( 'setText() : success' ); } catch ( e ) { $('#strftime-settext-4').html( e.message ); } try { $.strftime.setText({ 'test' : '' }); $('#strftime-settext-5').html( 'setText() : success' ); } catch ( e ) { $('#strftime-settext-5').html( e.message ); } try { $.strftime.setText({ 'format' : [ ] }); $('#strftime-settext-6').html( 'setText() : success' ); } catch ( e ) { $('#strftime-settext-6').html( e.message ); } try { $.strftime.setText({ 'days_short' : '' }); $('#strftime-settext-7').html( 'setText() : success' ); } catch ( e ) { $('#strftime-settext-7').html( e.message ); } try { $.strftime.setText({ 'days_short' : [ ] }); $('#strftime-settext-8').html( 'setText() : success' ); } catch ( e ) { $('#strftime-settext-8').html( e.message ); }
Results:
$.strftime.defaults() can be used to revert to the default text specifications.
$.strftime.defaults( ); $('#strftime-defaults-1').strftime( ); $('#strftime-defaults-2').strftime( '%A %d %B %Y' );
Results:
It is possible to get a formatted date and time directly, without using DOM elements. The $.strftime() function accepts the same parameters as the $.fn.strftime() function.
$('#strftime-direct').click(function() { alert( $.strftime( ) ); return false; });
Results:
In some cases, it is easier to pass parameters to strftime() using an object.
$('#strftime-obj-1').strftime( {} ); $('#strftime-obj-2').strftime( { format: '%H:%M:%S %m/%d/%Y' , dateTime : _dt }); $('#strftime-obj-3').strftime( { format: '%H:%M:%S %m/%d/%Y' , utc: true }); $('#strftime-obj-4').strftime( { dateTime : _dt });
Results:
It is possible to specify arbitrary dates for strftime() to display, including dates that wouldn't normally fit in a Date() object.
$('#strftime-dtobj-1').strftime( { dateTime : { H: 2 , M: 38 , S: 53 , Y: 1666 , m: 2 , d: 5 } } ); $('#strftime-dtobj-2').strftime( { dateTime : { H: 2 , M: 38 , S: 53 , Y: 1666 , m: 2 , d: 5 } , format: '%H:%M:%S %m/%d/%Y (%y)' } );
Results: