黑马程序员技术交流社区
标题:
关于计算日期的问题
[打印本页]
作者:
张荣耀
时间:
2013-9-5 00:05
标题:
关于计算日期的问题
编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,并且计算出是一年中的第几天
作者:
许庭洲
时间:
2013-9-5 06:20
protected void Button1_Click(object sender, EventArgs e)
{
string str = this.TextBox1.Text;
string year = str.Substring(0, 4);
string month = str.Substring(5, 2);
string Day = str.Substring(8, 2);
if (month.Substring(0, 1) == "0")
{
month = month.Substring(1, 1);
}
if (Day.Substring(0, 1) == "0")
{
Day = Day.Substring(1, 1);
}
this.TextBox1.Text = year + "年" + month + "月" + Day + "日";
//this.TextBox1.Text = year + "-" + month + "-" + Day + "-";}
作者:
科大涛哥
时间:
2013-9-5 08:07
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test10
{
class Program
{
//编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,
//程序计算这一天是星期几,并且计算出是一年中的第几天。
static bool yearflag;
static void Main(string[] args)
{
do
{
Console.WriteLine("按照" + "yyyy-MM-dd" + "的格式输入一个日期");
string date = Console.ReadLine();
string[] sdate = date.Split('-');
if (sdate.Length != 3)
{
Console.WriteLine("日期不合法");
continue;
}
//年份
if (Isint(sdate[0]) == 0)
{
Console.WriteLine("年份不合法");
continue;
}
if (Isint(sdate[0]) % 400 == 0 || Isint(sdate[0]) % 4 == 0 && Isint(sdate[0]) % 100 != 0)
{
yearflag = true;
}
else
{
yearflag = false;
}
//月份
if ( Isint(sdate[1]) < 1 || Isint(sdate[1]) > 12)
{
Console.WriteLine("月份不合法");
continue;
}
//天数
if (Isint(sdate[2]) == 0)
{
Console.WriteLine("天数不合法");
continue;
}
if (Isint(sdate[2]) < 1 || Isint(sdate[2]) > 31)
{
Console.WriteLine("天数应在1-31天之间");
continue;
}
else
{
if (Isint(sdate[1]) == 4 || Isint(sdate[1]) == 6 ||
Isint(sdate[1]) == 9 || Isint(sdate[1]) == 11)
{
if (Isint(sdate[2]) > 30)
{
Console.WriteLine("该月天数不等大于30天");
continue;
}
}
if (Isint(sdate[1]) == 2)
{
if (yearflag)
{
if (Isint(sdate[2]) > 29)
{
Console.WriteLine("闰年,2月天数不能大于29天");
continue;
}
}
else
{
if (Isint(sdate[2]) > 28)
{
Console.WriteLine("平年,2月天数不能大于28天");
continue;
}
}
}
}
Console.WriteLine("该日期是:"+ GetWeekDay(Isint(sdate[0]), Isint(sdate[1]), Isint(sdate[2])));
Console.WriteLine("该日期是第" + Countday(Isint(sdate[1])) + "天");
Console.ReadKey();
} while (true);
}
//计算出是一年中的第几天。
static int Countday(int date)
{
int d = 0;
yearflag = !yearflag;
if (date == 1)
{
d = date;
}
else if (date == 2)
{
d = 31 + date;
}
else if (date == 3)
{
if (yearflag)
{
d = 31 + 28 + date;
}
else
{
d = 31 + 29 + date;
}
}
else if (date == 4)
{
if (yearflag)
{
d = 31 + 28 + 31 + date;
}
else
{
d = 31 + 29 + 31 + date;
}
}
else if (date == 5)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + date;
}
}
else if (date == 6)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + date;
}
}
else if (date == 7)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + date;
}
}
else if (date == 8)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date;
}
}
else if (date == 9)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date;
}
}
else if (date == 10)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
}
}
else if (date == 11)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
}
}
else if (date == 12)
{
if (yearflag)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
}
}
return d;
}
//程序计算这一天是星期几
static string GetWeekDay(int y, int m, int d)
{
if (m == 1 || m == 2)
{
m += 12;
y--; //把一月和二月看成是上一年的十三月和十四月
}
// W = (Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + [ 13 * (M+1) / 5 ] + d.
int week = (y - 1 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + 13 * (m + 1) / 5 + d )%7;
string weekstr = "";
switch (week)
{
case 1: weekstr = "星期一"; break;
case 2: weekstr = "星期二"; break;
case 3: weekstr = "星期三"; break;
case 4: weekstr = "星期四"; break;
case 5: weekstr = "星期五"; break;
case 6: weekstr = "星期六"; break;
case 0: weekstr = "星期日"; break;
}
return weekstr;
}
//时间是否合法
static int Isint(string date)
{
try
{
int aa = Convert.ToInt32(date);
if (aa < 1)
{
Console.WriteLine("日期不合法");
return 0;
}
else
{
return aa;
}
}
catch
{
Console.WriteLine("日期不合法");
return 0;
}
}
}
}
作者:
马晓凤
时间:
2013-9-5 15:59
static void Main(string[] args)
{
//编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,并且计算出是一年中的第几天
while (true)
{
Console.WriteLine("请输入“yyyy-MM-dd”的格式输入一个日期");
DateTime newdate;
string date = Console.ReadLine();
if (DateTime.TryParse(date, out newdate))
{
int day = newdate.DayOfYear;
var week = newdate.DayOfWeek;
Console.WriteLine("是今年的第"+day + "天,今天是 " + week);
break;
}
else
{
Console.WriteLine("输入的日期格式不正确,请重新输入");
}
}
Console.ReadKey();
}
复制代码
作者:
小天
时间:
2013-9-5 16:43
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication7
{
class Program
{
public static int GetYear(string str)
{
List<int> list = new List<int>();
MatchCollection matches = Regex.Matches(str, @"\d+");
foreach (Match item in matches)
{
list.Add(Convert.ToInt32(item.Value));
}
return list[0];
}
public static int GetMonth(string str)
{
List<int> list = new List<int>();
MatchCollection matches = Regex.Matches(str, @"\d+");
foreach (Match item in matches)
{
list.Add(Convert.ToInt32(item.Value));
}
return list[1];
}
public static int GetDay(string str)
{
List<int> list = new List<int>();
MatchCollection matches = Regex.Matches(str, @"\d+");
foreach (Match item in matches)
{
list.Add(Convert.ToInt32(item.Value));
}
return list[2];
}
public static bool IsYear(string str)
{
int year = GetYear(str);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
return true;
}
return false;
}
public static string GetWeek(int day)
{
Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(0, "星期一");
dictionary.Add(1, "星期二");
dictionary.Add(2, "星期三");
dictionary.Add(3, "星期四");
dictionary.Add(4, "星期五");
dictionary.Add(5, "星期六");
dictionary.Add(6, "星期日");
if (dictionary.ContainsKey(day))
{
return dictionary[day];
}
return "未知的日期";
}
static void Main(string[] args)
{
string str = "2013-09-04";
bool flag = IsYear(str);
int days = GetDay(str);
int month=GetMonth(str);
for (int i = 1; i < month; i++)
{
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = days + 31;
break;
case 2:
if (flag == true)
{
days = days + 29;
}
else
{
days = days + 28;
}
break;
case 4:
case 6:
case 9:
case 11:
days = days + 30;
break;
default:
break;
}
}
string week = GetWeek(days % 7);
Console.WriteLine("{0}是一年中的第{1}天,这一天是{2}",str,days,week);
Console.ReadKey();
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2