Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- function isLeapYear( year ) {
- return ( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) );
- }
- function daysPerMonth( year, month ) {
- daysInMonth = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
- if ( month == -1 )
- month = 11
- days = daysInMonth[month];
- if ( month == 1 && isLeapYear( year ) )
- days++;
- return days;
- }
- function diffDateTime( dateA, dateB ) {
- maxValues = new Array( 0, 12, 0 );
- for ( i = 2; i >= 0; i-- ) {
- if ( dateA[i] < dateB[i] && i > 0 ) {
- dateA[i - 1]--;
- dateA[i] += maxValues[i] - dateB[i];
- if ( i == 2 )
- dateA[i] += daysPerMonth( dateA[0], dateA[1] );
- } else {
- dateA[i] -= dateB[i]
- }
- }
- return dateA;
- }
- function timeToString( value, str1, str2, str5 ) {
- if ( ! value )
- return 0;
- mod = value % 10;
- if ( value % 100 >= 10 && value % 100 <= 19 )
- return str5;
- if ( mod == 1 )
- return str1;
- if ( mod >= 2 && mod <= 4 )
- return str2;
- return str5;
- }
- function daysPassedText( year, month, day ) {
- tYear = timeToString( year, 'год', 'года', 'лет' );
- tMonth = timeToString( month, 'месяц', 'месяца', 'месяцев' );
- tDay = timeToString( day, 'день', 'дня', 'дней' );
- preValues = new Array( year, month, day );
- preTexts = new Array( tYear, tMonth, tDay );
- numPartsPresent = 0;
- for ( i = 2; i >= 0; i-- ) {
- if ( preValues[i] !=0 )
- numPartsPresent++;
- }
- text = '';
- for ( i = 2; i >= 0; i-- ) {
- if ( preValues[i] != 0 ) {
- text = preValues[i] + ' ' + preTexts[i] + text;
- if ( numPartsPresent > 1 ) {
- text = ' и ' + text;
- numPartsPresent = 0;
- } else if ( i > 0 && preValues[i - 1] != 0 ) {
- text = ', ' + text;
- }
- }
- }
- return text;
- }
- now = new Date();
- foundationDate = new Date( 'March 1, 2010' );
- dateA = new Array( now.getFullYear(), now.getMonth(), now.getDate() );
- dateB = new Array( foundationDate.getFullYear(), foundationDate.getMonth(), foundationDate.getDate() );
- dateC = diffDateTime( dateA, dateB );
- document.write( 'Империя живёт уже <b>' + daysPassedText( dateC[0], dateC[1], dateC[2] ) + '</b>.' );
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement