计算工作日
.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 ?>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构