참나...간단한 날짜 변환거 가지고 삽질하다가 찾아서 퍼왔네...
--------------------------------------------------------------------------------
-- Date Add, Adddays, Addmonths
--------------------------------------------------------------------------------
DateTime dtTomorrow;
dtTomorrow = DateTime.Today.AddDays(1);
--------------------------------------------------------------------------------
DateTime dtTomorrow;
TimeSpan tsOneDay;
tsOneDay = new TimeSpan(1, 0, 0, 0);
dtTomorrow = DateTime.Today.Add(tsOneDay);
AddMonths(1);
--------------------------------------------------------------------------------
int intDuration;
TimeSpan tsDuration;
tsDuration = new DateTime(2002, 2, 15) - new DateTime(2002, 2, 10);
intDuration = tsDuration.Days;
--------------------------------------------------------------------------------
-- Date Convert
--------------------------------------------------------------------------------
DateTime x = DateTime.Now.Date;
TextBox1.Text = x.ToString();
string s=TextBox1.Text;
TextBox2.Text=Convert.ToDateTime(s).ToString("yyyy-MM-dd");
//for example input date current date value is --- 21-12-2007 00:00:00
//Converted format output is ----- 2007-12-21
--------------------------------------------------------------------------------
DateTime date1, date2;
bool date1OK, date2OK;
date1 = new DateTime(1,1,1);
date2 = new DateTime(1,1,1);
try {
date1 = Convert.ToDateTime(Date1.Text);
date1OK=true;
}
catch {
date1OK = false;
}
--------------------------------------------------------------------------------
//Date format conversion
//e.g. 23/05/2007 into 2007-05-23
String FromDate = Txt_FromDate.Text;
DateTime DT_FromDate = DateTime.Parse(FromDate);
FromDate = DT_FromDate.ToString("yyyy-MM-dd");
--------------------------------------------------------------------------------
DateTime MyDateTime = Convert.ToDateTime("16:25:05");
lblDateOut.Text = Convert.ToString(MyDateTime);
--------------------------------------------------------------------------------
time.Text=DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
--------------------------------------------------------------------------------
In that case write a simple utility (I have them for integers, decimal etc) that uses regular expressions to validate the date
Regx.ValidationExpression=@"^(([1-9])|(0[1-9])|(1[0-2]))\/((0[1-9])|([1-31]))\/((\d{2})|(\d{4}))$";
if it is a match then it is a date and so can perform quickly the day is valid for the month. It will still be a lot quicker than try catch blocks
--------------------------------------------------------------------------------
-- Date Add, Adddays, Addmonths
--------------------------------------------------------------------------------
DateTime dt = DateTime.Now.Date;
Response.Write("DateTime.Now.Date = " + dt.ToString() + "<br/>");
string strDate = "2007/05/01";
Response.Write("Convert.ToDateTime(\"2007/05/01\") = " + Convert.ToDateTime(strDate).ToString() + "<br/>");
string strDate1 = "2007.05.01";
Response.Write("Convert.ToDateTime(\"2007.05.01\") = " + Convert.ToDateTime(strDate1).ToString() + "<br/>");
string strDate2 = "2007-05-01";
Response.Write("Convert.ToDateTime(\"2007-05-01\") = " + Convert.ToDateTime(strDate2).ToString() + "<br/>");
string strDate3 = "2007 05 01";
Response.Write("Convert.ToDateTime(\"2007 05 01\") = " + Convert.ToDateTime(strDate3).ToString() + "<br/>");
출력 )
DateTime.Now.Date = 2007-05-22 오전 12:00:00
Convert.ToDateTime("2007/05/01") = 2007-05-01 오전 12:00:00
Convert.ToDateTime("2007.05.01") = 2007-05-01 오전 12:00:00
Convert.ToDateTime("2007-05-01") = 2007-05-01 오전 12:00:00
Convert.ToDateTime("2007 05 01") = 2007-05-01 오전 12:00:00
결과 )
[/],[.],[-],[ ] 문자들로 구분을 지어주면 DateTime 형태로 변환이 가능하나
[,], [%],[\],[_],[^]... 등등의 기호와 일반적으로 Sql 에서 변환이 가능한 숫자 8자리는 변경이 되질 않았다.