日期的計算跟顯示我想可能在所有的系統都會用到的啦
而且要處理的方式還是有很多的不同
光是要計算日期區間可能就有很多人很多方式了
最近剛好看到就去找了一些方式小小整理了一下下
方便日後使用...不足的Google一下我想一定是沒有問題
我把找到的或是有用到的都轉成C#的呈現了
下面是我把整個.cs檔都貼上了
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.VisualBasic;
using System.Globalization;
public partial class DemoDateTime : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//取目前週數
Response.Write(GetWeekOfNow() + "
");
Response.Write(GetWeekOfNow2() + "
");
Response.Write(GetWeekOfNow3() + "
");
//VB.NET的取週數好像更簡單就這麼一行就結束了
//Response.Write(DatePart(DateInterval.WeekOfYear, DateTime.Today))
//取得月數
TimeSpan ts = new TimeSpan(Convert.ToDateTime("2010/6/30").Ticks - Convert.ToDateTime("2010/1/1").Ticks);
Response.Write(Math.Round((ts.TotalDays / 365) * 12, 2) + "
");
//取得月數
Double tsd = (ts.TotalDays / 365) * 12;
Response.Write(tsd.ToString("#.#0") + "
");
//取得月數
Double diff = DateDiff("Month", Convert.ToDateTime("2010/1/1"), Convert.ToDateTime("2010/6/30"));
Response.Write(diff + "
");
//取得月數
Double diffvb = DateDiffvb(Microsoft.VisualBasic.DateInterval.Month, Convert.ToDateTime("2010/1/1"), Convert.ToDateTime("2010/6/30"));
Response.Write(diffvb + "
");
//取得月數
Double diff2 = DateDiff2(DateInterval.Month, Convert.ToDateTime("2010/1/1"), Convert.ToDateTime("2010/6/30"));
Response.Write(diff2 + "
");
//取得天數
int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
Response.Write(days + "
");
//取得二個日期相差時間
DateTime objStartTime = new DateTime(2010, 1, 1, 0, 0, 0);
DateTime objEndTime = new DateTime(2010, 6, 9, 0, 0, 0);
TimeSpan objTimeSpan = objEndTime.Subtract(objStartTime);
string strDiff = string.Empty;
strDiff = "相差:" + objTimeSpan.Days.ToString() + "天
或"
+ (objTimeSpan.Days * 24) + "小時
或"
+ (objTimeSpan.Days * 24 * 60) + "分鍾
或"
+ (objTimeSpan.Days * 24 * 60 * 60) + "秒
";
Response.Write(strDiff + "
");
//國家日期格式設定CultureInfo
CultureInfo ci = new CultureInfo("zh-tw");
System.Globalization.Calendar cal = new TaiwanCalendar();
ci.DateTimeFormat.Calendar = cal;
DateTime dt = DateTime.Parse("2010/06/09");
string strWareki = dt.ToString("ggyy/MM/dd", ci);
Response.Write(strWareki + "
");
// Result: 中華民國99/06/09
ci.DateTimeFormat.Calendar = cal;
string strWareki2 = "中華民國99/06/09";
string strDate = DateTime.ParseExact(strWareki2, "ggyy/MM/dd", ci).ToString("yyyy/MM/dd");
Response.Write(strDate + "
");
// Result: 2010/06/09
System.Globalization.CultureInfo cuinfo = new System.Globalization.CultureInfo("zh-TW",true);
cuinfo.DateTimeFormat.Calendar = cuinfo.OptionalCalendars[2];
// [0]『西曆(中文)』,Calendar Type : "System.Globalization.GregorianCalendar"
// [1]『西曆(英文)』,Calendar Type : "System.Globalization.GregorianCalendar"
// [2]『中華民國曆』,Calendar Type : "System.Globalization.TaiwanCalendar"
System.Threading.Thread.CurrentThread.CurrentCulture = cuinfo;
System.Threading.Thread.CurrentThread.CurrentUICulture = cuinfo;
string strNonDate = DateTime.Now.ToString(cuinfo);
Response.Write(strNonDate + "
");
// Result: 99/6/9 下午 03:34:23
}
public static int GetWeekOfNow()
{
DateTime dt = DateTime.Now.Date;
int weeknow = Convert.ToInt32(dt.DayOfWeek);
int daydiff = (-1) * (weeknow + 1);
int days = System.DateTime.Now.AddDays(daydiff).DayOfYear;
int weeks = days / 7;
if (days % 7 != 0)
{
weeks++;
}
return weeks;
}
private int GetWeekOfNow2(DateTime CurrDate)
{
DateTime dtYear = new DateTime(CurrDate.Year, 1, 1);
int dtStarWeek = (int)dtYear.DayOfWeek;
int dtDays = CurrDate.DayOfYear + dtStarWeek;
if (dtDays < 7)
return 1;
int dtValue = (dtDays / 7);
if (dtDays % 7 != 0)
dtValue++;
return dtValue;
}
private string GetWeekOfNow3()
{
System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(DateTime.Now, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
string week = Convert.ToString(weekOfYear);
if (week.Length == 1)
week = '0' + week;
return week;
}
private double DateDiff(string DateType, DateTime startDate, DateTime endDate)
{
double diff = 0.00;
System.TimeSpan TS = new System.TimeSpan(endDate.Ticks - startDate.Ticks);
switch (DateType.ToUpper())
{
case "YEAR":
diff = Convert.ToDouble(TS.TotalDays / 365);
break;
case "MONTH":
diff = Convert.ToDouble((TS.TotalDays / 365) * 12);
break;
case "DAY":
diff = Convert.ToDouble(TS.TotalDays);
break;
case "HOUR":
diff = Convert.ToDouble(TS.TotalHours);
break;
case "MINUTE":
diff = Convert.ToDouble(TS.TotalMinutes);
break;
case "SECOND":
diff = Convert.ToDouble(TS.TotalSeconds);
break;
}
return diff;
}
public double DateDiffvb(DateInterval Interval, DateTime startDate, DateTime endDate)
{
return Microsoft.VisualBasic.DateAndTime.DateDiff(Interval, startDate, endDate,
Microsoft.VisualBasic.FirstDayOfWeek.System, Microsoft.VisualBasic.FirstWeekOfYear.System);
}
public static long DateDiff2(DateInterval Interval, System.DateTime StartDate, System.DateTime EndDate)
{
long lngDateDiffValue = 0;
System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
switch (Interval)
{
case DateInterval.Second:
lngDateDiffValue = (long)TS.TotalSeconds;
break;
case DateInterval.Minute:
lngDateDiffValue = (long)TS.TotalMinutes;
break;
case DateInterval.Hour:
lngDateDiffValue = (long)TS.TotalHours;
break;
case DateInterval.Day:
lngDateDiffValue = (long)TS.Days;
break;
case DateInterval.Weekday:
lngDateDiffValue = (long)(TS.Days / 7);
break;
case DateInterval.Month:
lngDateDiffValue = (long)(TS.Days / 30);
break;
case DateInterval.Quarter:
lngDateDiffValue = (long)((TS.Days / 30) / 3);
break;
case DateInterval.Year:
lngDateDiffValue = (long)(TS.Days / 365);
break;
}
return (lngDateDiffValue);
}
}
文章標籤
全站熱搜

大大你好,最近在寫醫院需要的程式,因為醫院好像都習慣使用 民國日期,看了你的這篇文章後才知道,原來有using System.Globalization;可以使用 受益良多,謝謝