Posted By

bullzito on 12/07/10


Tagged

javascript-month-day-year-date-jquery-dates-Expiration


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bullzito


Change or Update HTML By Comparing Expiration Dates


 / Published in: JavaScript
 

URL: http://marioluevanos.com/playground/Compare%20Expiration%20Dates/

Change HTML content in your HTML by comparing expiration dates. I needed code to update ads on my site. Ads had an expiration dates so this is what I used.

  1. var now = new Date();
  2. var year, month, day, mins, sec;
  3.  
  4. //get the current date //add a zero for single digit values
  5. year = now.getFullYear(); if (year < 1000) {year +- 1900;}
  6. month = now.getMonth() + 1; if (month < 10) {month = "0" + month;}
  7. day = now.getDate(); if (day < 10) {day = "0" + day;}
  8. hrs = now.getHours(); if (hrs < 10) {day = "0" + hrs;}
  9. mins = now.getMinutes(); if (mins < 10) {mins = "0" + mins;}
  10. sec = now.getSeconds(); if (sec < 10) {sec = "0" + sec;}
  11.  
  12. //concatenate the dates into a string
  13. var current = String(year) + String(month) + String(day) + String(hrs) + String(mins) + String(sec);
  14. //convert current time(string) to a number to compare values
  15. var curr = Number(current);
  16.  
  17. // dates represent expiration dates (YYYY MM DD HH MM SS)
  18. var A = 20101203160800;
  19. var B = 20101203161400;
  20. var C = 20101203152900;
  21.  
  22. if (A > curr) {
  23. $('#status').html('This is A');
  24. }
  25. else if (B > curr) {
  26. $('#status').html('This is B');
  27. }
  28. else if (C > curr) {
  29. $('#status').html('This is C');
  30. }
  31. else {
  32. $('#status').html('Everything Has Expired');
  33. }

Report this snippet  

You need to login to post a comment.