Advertisement
SergeyBiryukov

Date Diff Text

Apr 3rd, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. function isLeapYear( year ) {
  3.     return ( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) );
  4. }
  5.  
  6. function daysPerMonth( year, month ) {
  7.     daysInMonth = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
  8.  
  9.     if ( month == -1 )
  10.         month = 11
  11.  
  12.     days = daysInMonth[month];
  13.     if ( month == 1 && isLeapYear( year ) )
  14.         days++;
  15.  
  16.     return days;
  17. }
  18.  
  19. function diffDateTime( dateA, dateB ) {
  20.     maxValues = new Array( 0, 12, 0 );
  21.  
  22.     for ( i = 2; i >= 0; i-- ) {
  23.         if ( dateA[i] < dateB[i] && i > 0 ) {
  24.             dateA[i - 1]--;
  25.             dateA[i] += maxValues[i] - dateB[i];
  26.             if ( i == 2 )
  27.                 dateA[i] += daysPerMonth( dateA[0], dateA[1] );
  28.         } else {
  29.             dateA[i] -= dateB[i]
  30.         }
  31.     }
  32.  
  33.     return dateA;
  34. }
  35.  
  36. function timeToString( value, str1, str2, str5 ) {
  37.     if ( ! value )
  38.         return 0;
  39.  
  40.     mod = value % 10;
  41.  
  42.     if ( value % 100 >= 10 && value % 100 <= 19 )
  43.         return str5;
  44.  
  45.     if ( mod == 1 )
  46.         return str1;
  47.  
  48.     if ( mod >= 2 && mod <= 4 )
  49.         return str2;
  50.  
  51.     return str5;
  52. }
  53.  
  54. function daysPassedText( year, month, day  ) {
  55.     tYear  = timeToString( year, 'год', 'года', 'лет' );
  56.     tMonth = timeToString( month, 'месяц', 'месяца', 'месяцев' );
  57.     tDay   = timeToString( day, 'день', 'дня', 'дней' );
  58.  
  59.     preValues = new Array( year, month, day );
  60.     preTexts = new Array( tYear, tMonth, tDay );
  61.  
  62.     numPartsPresent = 0;
  63.     for ( i = 2; i >= 0; i-- ) {
  64.         if ( preValues[i] !=0 )
  65.             numPartsPresent++;
  66.     }
  67.  
  68.     text = '';
  69.     for ( i = 2; i >= 0; i-- ) {
  70.         if ( preValues[i] != 0 ) {
  71.             text = preValues[i] + ' ' + preTexts[i] + text;
  72.             if ( numPartsPresent > 1 ) {
  73.                 text = ' и ' + text;
  74.                 numPartsPresent = 0;
  75.             } else if ( i > 0 && preValues[i - 1] != 0 ) {
  76.                 text = ', ' + text;
  77.             }
  78.         }
  79.     }
  80.  
  81.     return text;
  82. }
  83.  
  84. now = new Date();
  85. foundationDate = new Date( 'March 1, 2010' );
  86.  
  87. dateA = new Array( now.getFullYear(), now.getMonth(), now.getDate() );
  88. dateB = new Array( foundationDate.getFullYear(), foundationDate.getMonth(), foundationDate.getDate() );
  89. dateC = diffDateTime( dateA, dateB );
  90.  
  91. document.write( 'Империя живёт уже <b>' + daysPassedText( dateC[0], dateC[1], dateC[2] ) + '</b>.' );
  92. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement