网站运行时间代码

时间:2022-09-20 23:28:09

样例 1

  1. <span>
  2. 本站已运行
  3. </span>
  4. <span id="span_dt_dt">
  5. </span>
  6. <script>
  7. /*建站时间*/
  8. function show_date_time() {
  9. window.setTimeout("show_date_time()", 1e3);
  10. var BirthDay = new Date("2018/03/01"),
  11. today = new Date,
  12. timeold = today.getTime() - BirthDay.getTime(),
  13. msPerDay = 864e5,
  14. e_daysold = timeold / msPerDay,
  15. daysold = Math.floor(e_daysold),
  16. e_hrsold = 24 * (e_daysold - daysold),
  17. hrsold = Math.floor(e_hrsold),
  18. e_minsold = 60 * (e_hrsold - hrsold),
  19. minsold = Math.floor(60 * (e_hrsold - hrsold)),
  20. seconds = Math.floor(60 * (e_minsold - minsold));
  21. span_dt_dt.innerHTML = daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
  22. }
  23. show_date_time();
  24. </script>

样例 2

  1. 当前系统时间
  2. <span id="nowTime"></span>
  3. <script type="text/javascript">
  4. //获取系统时间
  5. var newDate = '';
  6. getLangDate();
  7. //值小于10时,在前面补0
  8. function dateFilter(date){
  9. if(date < 10){return "0"+date;}
  10. return date;
  11. }
  12. function getLangDate(){
  13. var dateObj = new Date(); //表示当前系统时间的Date对象
  14. var year = dateObj.getFullYear(); //当前系统时间的完整年份值
  15. var month = dateObj.getMonth()+1; //当前系统时间的月份值
  16. var date = dateObj.getDate(); //当前系统时间的月份中的日
  17. var day = dateObj.getDay(); //当前系统时间中的星期值
  18. var weeks = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
  19. var week = weeks[day]; //根据星期值,从数组中获取对应的星期字符串
  20. var hour = dateObj.getHours(); //当前系统时间的小时值
  21. var minute = dateObj.getMinutes(); //当前系统时间的分钟值
  22. var second = dateObj.getSeconds(); //当前系统时间的秒钟值
  23. var timeValue = "" +((hour >= 12) ? (hour >= 18) ? "晚上" : "下午" : "上午" ); //当前时间属于上午、晚上还是下午
  24. newDate = dateFilter(year)+"年"+dateFilter(month)+"月"+dateFilter(date)+"日 "+" "+dateFilter(hour)+":"+dateFilter(minute)+":"+dateFilter(second);
  25. document.getElementById("nowTime").innerHTML = timeValue+"好!当前时间为: "+newDate+" "+week;
  26. setTimeout("getLangDate()",1000);
  27. }
  28. </script>

样例三

  1. function showtime(times) { //传入时间的参数
  2. var time_1 = new Date(times).getTime();
  3. var time_2 = new Date().getTime();
  4. var time_3;
  5. var time_distance;
  6. //计算时间差
  7. if (time_1 > time_2) {
  8. time_distance = time_1 - time_2;
  9. } else {
  10. time_distance = time_2 - time_1;
  11. }
  12. if (time_distance > 0) {
  13. // 天时分秒换算
  14. var int_day = Math.floor(time_distance / 86400000);
  15. time_distance -= int_day * 86400000;
  16. var int_hour = Math.floor(time_distance / 3600000);
  17. time_distance -= int_hour * 3600000;
  18. var int_minute = Math.floor(time_distance / 60000);
  19. time_distance -= int_minute * 60000;
  20. var int_second = Math.floor(time_distance / 1000);
  21. // 时分秒为单数时、前面加零
  22. if (int_day < 10) {
  23. int_day = "0" + int_day;
  24. }
  25. if (int_hour < 10) {
  26. int_hour = "0" + int_hour;
  27. }
  28. if (int_minute < 10) {
  29. int_minute = "0" + int_minute;
  30. }

转载:https://blog.bnightning.cn/82.html