计算工作日

.net

 1 static void Main(string[] args)
 2 {
 3     DateTime sdt = DateTime.Now;
 4     DateTime edt = new DateTime(2016, 2, 7);
 5 
 6     int d = (edt - sdt).Days;
 7 
 8     int wd = 0;
 9     while(sdt < edt)
10     {
11         sdt = sdt.AddDays(1);
12         if (sdt.DayOfWeek != DayOfWeek.Saturday && sdt.DayOfWeek != DayOfWeek.Sunday)
13         {
14             ++wd;
15         }
16     }
17 
18     Console.WriteLine(d.ToString());
19     Console.WriteLine(wd.ToString());
20     Console.ReadLine();
21 }

c++

 1 #include "stdafx.h"
 2 #include "time.h"
 3 
 4 int main()
 5 {
 6     time_t st;
 7     time(&st);
 8 
 9     struct tm tmp = { 0 };
10     tmp.tm_year = 2016 -  1900;
11     tmp.tm_mon = 2 - 1;
12     tmp.tm_mday = 7;
13     time_t et = mktime(&tmp);
14 
15     double d = difftime(et, st) / (60 * 60 * 24);
16 
17     int i = 0, wd = 0;
18     struct tm _tm;
19     time_t _t;
20     while ( i < d) {
21         i++;
22         _t = st + (60 * 60 * 24 * i);
23         gmtime_s(&_tm, &_t);
24 
25         if (_tm.tm_wday != 5 && _tm.tm_wday != 6) {
26             ++wd;
27         }
28     }
29 
30     printf("%.f\n", d);
31     printf("%d\n", wd);
32     scanf_s("");
33 }

java

 1 public static void main(String[] args) throws Exception {
 2     Date sdt = new Date();
 3     Date edt = new SimpleDateFormat("yyyy-MM-dd").parse("2016-2-7 00:00:00");
 4     
 5     Calendar scal = Calendar.getInstance();
 6     scal.setTime(sdt);
 7     Calendar ecal = Calendar.getInstance();
 8     ecal.setTime(edt);
 9     
10     long d = (ecal.getTimeInMillis() - scal.getTimeInMillis()) / (60 * 60 * 24 * 1000);
11     
12     int wd = 0;
13     while(scal.getTimeInMillis() < ecal.getTimeInMillis()) {
14         scal.add(Calendar.DAY_OF_MONTH, 1);
15         if(scal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY 
16                 && scal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
17             ++wd;
18         }
19     }
20     
21     System.out.println(d);
22     System.out.println(wd);
23 }

js

 1 <script type="text/javascript">
 2     function init() {
 3         var sdt = new Date();
 4         var edt = new Date('2016-2-7'.replace(/-/g, '/'));
 5         var d = parseInt((edt - sdt) / (60 * 60 * 24 * 1000));
 6 
 7         var wd = 0;
 8         while (sdt < edt) {
 9             var timestamp = Date.parse(sdt) + (60 * 60 * 24 * 1000);
10             sdt = new Date(timestamp);
11             
12             if (sdt.getDay() != 5 && sdt.getDay() != 6) {
13                 ++wd;
14             }
15         }
16         alert(d);
17         alert(wd);
18     }
19 </script>

 php

 1 <?php
 2     date_default_timezone_set("PRC");
 3     
 4     $sdt = time();
 5     $edt = mktime(0, 0, 0, 2, 7, 2016);
 6     
 7     $d = ($edt - $sdt) / (60 * 60 * 24);
 8     
 9     $wd = 0;
10     while($sdt < $edt) {
11         $sdt += (60 * 60 * 24 * 1);
12         $week = date("N", $sdt);
13         
14         if($week != 5 && $week != 6) {
15             $wd++;
16         }
17     }
18     
19     echo sprintf("%d\n", $d);
20     echo $wd;
21 ?>

 

posted @ 2015-11-09 15:15  猫不理饼  阅读(439)  评论(0编辑  收藏  举报