asp.net常用的验证和获取日期的方法
public string GetWeekOfFirstDay()
{
string ReasonTime;
switch (DateTime.Now.Date.DayOfWeek.ToString())
{
case "Monday":
ReasonTime=DateTime.Now.Date.ToString("yyyy-MM-dd");
return ReasonTime;
case "Tuesday":
ReasonTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
return ReasonTime;
case "Wednesday":
ReasonTime = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd");
return ReasonTime;
case "Thursday":
ReasonTime = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
return ReasonTime;
case "Friday":
ReasonTime = DateTime.Now.AddDays(-4).ToString("yyyy-MM-dd");
return ReasonTime;
case "Saturday":
ReasonTime = DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd");
return ReasonTime;
case "Sunday":
ReasonTime = DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd");
return ReasonTime;
default:
return DateTime.Now.Date.DayOfWeek.ToString("yyyy-MM-dd");
}
}// end GetWeekOfFirstDay()
public string TheFirstDayOfMonth()
{
int DayOfMonth = DateTime.Now.Day;
return DateTime.Now.AddDays(-DayOfMonth + 1).ToString("yyyy-MM-dd");
}
public string TheEndDayOfMonth()
{
int aMonthHaveDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
int DayOfMonth = DateTime.Now.Day;
int TheEndOfMonth = aMonthHaveDays - DayOfMonth;
return DateTime.Now.AddDays(TheEndOfMonth).ToString("yyyy-MM-dd");
}
public string TheFirstDayOfYear()
{//return2007-1-1
int dayOfYear = DateTime.Now.DayOfYear;
return DateTime.Now.AddDays(-dayOfYear + 1).ToString("yyyy-MM-dd");
}
public string TheEndDayOfYear()
{//return 2007-12-31
int month22HaveDays=DateTime.DaysInMonth(DateTime.Now.Year,12);
return DateTime.Now.Year + "-12-" + month22HaveDays;
}
public string GetDayAddWeekofDay(string strTime)
{//return 2007-3-3星期三
string Time = CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(Convert.ToDateTime(strTime)).ToString();
strTime = Convert.ToDateTime(strTime).ToString("yyyy-MM-dd");
switch (Time)
{
case "Monday":
strTime = strTime + "(星期一)";
return strTime;
case "Tuesday":
strTime = strTime + "(星期二)";
return strTime;
case "Wednesday":
strTime = strTime + "(星期三)";
return strTime;
case "Thursday":
strTime = strTime + "(星期四)";
return strTime;
case "Friday":
strTime = strTime + "(星期五)";
return strTime;
case "Saturday":
strTime = strTime + "(星期六)";
return strTime;
case "Sunday":
strTime = strTime + "(星期日)";
return strTime;
default:
return strTime;
}
}//end GetDayAddWeekofDay(string strTime)
public string GetFormatTime(string time)
{//return yyyy-MM-dd
if (time == "")
{
return "";
}
else
{
return Convert.ToDateTime(time).ToString("yyyy-MM-dd");
}
}
public string GetFormatMinutes(string time)
{
if (time == "")
{
return "";
}
else
{
return Convert.ToDateTime(time).ToString("yyyy-MM-dd HH:mm");
}
}
public bool RegexLongTime(string time)
{//yyyy-MM-dd hh:mm:ss
bool tureTime;
tureTime = System.Text.RegularExpressions.Regex.IsMatch(time, @"^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$");
return tureTime;
}
public bool RegexDate(string time)
{//yyyy-MM-dd
bool reasonTime;
string regex = @"^(19|20)\d\d[- /.](0[1-9]|1[012]|[1-9])[- /.](0[1-9]|[12][0-9]|3[01]|[1-9])$";
reasonTime = System.Text.RegularExpressions.Regex.IsMatch(time,regex);
return reasonTime;
}
public bool RegexInt32(string strInt)
{//int32
return System.Text.RegularExpressions.Regex.IsMatch(strInt, @"^\d+$");
}
我们提供的服务有:成都网站建设、网站设计、微信公众号开发、网站优化、网站认证、昌江ssl等。为千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的昌江网站制作公司
本文标题:asp.net常用的验证和获取日期的方法
当前路径:http://scjbc.cn/article/isphge.html