---
title: "对任何一天是星期几算法的实现 "
author: "funjackyone"
date: "2007-10-16T09:17:07Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-10-16T09:17:07Z"
id: 132173
url: "https://funjackyone.iteye.com/blog/132173"
---

方法1：

private string str\_week(int y,int m,int d){    
   int a=7; // 用来保存计算得到的星期几的整数  
   string str\_date="";  
   if((m==1)||(m==2))//如果是一月或二月进行换算  
   {  
    m+=12;  
    y--;  
   }  
   a=(d+2\*m+3\*(m+1)/5+y+y/4-y/100+y/400)%7;   //得到的星期几的整数  
   switch (a){   
    case 0:  
     str\_date="星期一";  
     break;  
    case 1:  
     str\_date="星期二";  
     break;  
    case 2:  
     str\_date="星期三";  
     break;  
    case 3:  
     str\_date="星期四";  
     break;  
    case 4:  
     str\_date="星期五";  
     break;  
    case 5:  
     str\_date="星期六";  
     break;  
    case 6:  
     str\_date="星期日";  
     break;  
   }  
   return str\_date;   
}

方法2：

private string str\_week(int y,int m,int d){

string str\_date = y.ToString() + "-" + m.ToString() + "-" + d.ToString();

return DateTime.Parse(str\_date ).ToString("ddd",new System.Globalization.CultureInfo("zh-cn"))  //返回 zh-cn区域的星期

}