forked from Botanical/BotanJS
141 lines
3.7 KiB
JavaScript
141 lines
3.7 KiB
JavaScript
(function(){
|
|
var ns = __namespace( "Astro.utils.Date" );
|
|
|
|
//// Static utilities
|
|
// Chinese char for 0-9
|
|
var cChar = [ "12295", "19968", "20108", "19977", "22235", "20116", "20845", "19971", "20843", "20061", "21313" ];
|
|
// Capital month
|
|
var CAP_MONTHS = [ false, false, false, true, false, true, false, false, true, false, true, false ];
|
|
var MONTH_ABBR = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
var MONTH = [
|
|
"January"
|
|
, "February"
|
|
, "March"
|
|
, "April"
|
|
, "May"
|
|
, "June"
|
|
, "July"
|
|
, "August"
|
|
, "September"
|
|
, "October"
|
|
, "November"
|
|
, "December"
|
|
];
|
|
var DAY_ABBR = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
var DAY = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
|
|
|
// get responding chinese char from number
|
|
var cCountDay = function (num)
|
|
{
|
|
str = num.toString();
|
|
if( num != 0 && num % 10 == 0 )
|
|
{
|
|
return ( num == 10 ? "" : String.fromCharCode( cChar[ str[0] ] ) )
|
|
+ String.fromCharCode( cChar[10] );
|
|
}
|
|
else if( num < 10 )
|
|
{
|
|
return String.fromCharCode( cChar[ str[0] ] );
|
|
}
|
|
else if( num < 20 )
|
|
{
|
|
return String.fromCharCode( cChar[10] )
|
|
+ String.fromCharCode( cChar[ str[1] ] );
|
|
}
|
|
|
|
return String.fromCharCode( cChar[ str[0] ] )
|
|
+ String.fromCharCode( cChar[10] )
|
|
+ String.fromCharCode( cChar[ str[1] ] );
|
|
};
|
|
|
|
|
|
// Input yyyy-mm-dd small small
|
|
// output dd mmmm yyyy DDDDD
|
|
var prettyDay = function ( b, sMon, sDay )
|
|
{
|
|
var c = b.split("-");
|
|
var d = new Date( c[0], c[1] - 1, c[2] );
|
|
|
|
return d.getDate() + " "
|
|
+ ( sMon ? MONTH_ABBR[ d.getMonth() ] : MONTH[ d.getMonth() ] ) + " "
|
|
+ d.getFullYear() + " "
|
|
+ ( sDay ? DAY_ABBR[ d.getDay() ] : DAY[ d.getDay() ] )
|
|
;
|
|
};
|
|
|
|
// Small month stamp: output dd-mmm-yyyy
|
|
var smstamp = function ( dateObj )
|
|
{
|
|
return dateObj.getDate() + "-" + MONTH_ABBR[ dateObj.getMonth() ] + "-" + dateObj.getFullYear();
|
|
};
|
|
|
|
// Pretty format XXth MMMM, YYYY
|
|
var pretty = function( dateObj, plain )
|
|
{
|
|
return String( dateObj.getDate() )
|
|
+ ( plain ? "" : "<sup>" )
|
|
+ getOrdinalSuffix( dateObj.getDate() )
|
|
+ ( plain ? " " : "</sup> " )
|
|
+ MONTH[ dateObj.getMonth() ]
|
|
+ ", "
|
|
+ String( dateObj.getFullYear() )
|
|
;
|
|
};
|
|
|
|
// returns a timespan
|
|
// Input Array [ yyyy, mm, dd ], [ yyyy, mm, dd ]
|
|
var diff = function( from, to )
|
|
{
|
|
return new Date(
|
|
new Date( to[0], to[1], to[2] ).getTime()
|
|
- new Date( from[0], from[1], from[2] ).getTime()
|
|
);
|
|
};
|
|
|
|
// Get a chinese date format
|
|
var chinese = function ( date )
|
|
{
|
|
var str = date.getFullYear().toString();
|
|
var datestmp = "";
|
|
|
|
for(i in str)
|
|
{
|
|
datestmp += String.fromCharCode( cChar[ str[i] ] );
|
|
}
|
|
|
|
datestmp += String.fromCharCode("24180");
|
|
|
|
datestmp += cCountDay(date.getMonth() + 1)
|
|
+ String.fromCharCode("26376")
|
|
+ cCountDay(date.getDate())
|
|
+ String.fromCharCode("26085");
|
|
|
|
return datestmp;
|
|
};
|
|
|
|
// returns a suffix string
|
|
// st, nd, rd, th
|
|
var getOrdinalSuffix = function( day )
|
|
{
|
|
if( day < 1 || 31 < day )
|
|
throw new Error( "Day is out of range 1 <= day <= 31" );
|
|
|
|
if ( day == 1 || day == 21 || day == 31 ) return "st";
|
|
else if ( day == 2 || day == 22 ) return "nd";
|
|
else if ( day == 3 || day == 23 ) return "rd";
|
|
return "th";
|
|
};
|
|
|
|
ns[ NS_EXPORT ]( EX_FUNC, "pretty", pretty );
|
|
ns[ NS_EXPORT ]( EX_FUNC, "prettyDay", prettyDay );
|
|
ns[ NS_EXPORT ]( EX_FUNC, "diff", diff );
|
|
ns[ NS_EXPORT ]( EX_FUNC, "smstamp", smstamp );
|
|
ns[ NS_EXPORT ]( EX_FUNC, "chinese", chinese );
|
|
|
|
ns[ NS_EXPORT ]( EX_CONST, "CAP_MONTHS", CAP_MONTHS );
|
|
ns[ NS_EXPORT ]( EX_CONST, "MONTH", MONTH );
|
|
ns[ NS_EXPORT ]( EX_CONST, "MONTH_ABBR", MONTH_ABBR );
|
|
ns[ NS_EXPORT ]( EX_CONST, "DAY", DAY );
|
|
ns[ NS_EXPORT ]( EX_CONST, "DAY_ABBR", DAY_ABBR );
|
|
})();
|