2014年7月13日 星期日

[RESOLVED] datetime format


i was working my projct in vs 2010 and now im transfering my project to vs2012


i have a date control which saves date in te format dd/mm/yyyy in the database.


when calling that date it's returning it as mm/dd/yyyy


i want a function that transforms the date retrieved from the database which is (dd/mm/yyyy) into the format (mm/dd/yyyy)


this is my control: 







this is my loading function:




private void LoadRuling(int RulingID, string language)

{


.


.


.

if (rul_entries.RulingDate != null)

{

txt_date1.GregorianDateText = obj_str.Entry(1, rul_entries.RulingDate.ToString(), ' ');

}


.


.


.


}


i have this function also:


public DateTime GregorianDate

{

get

{

if (txtDate.Text == "") return DateTime.MinValue;


DateTime selectedDate = DateTime.MinValue;

System.Globalization.CultureInfo format = new System.Globalization.CultureInfo("en-US");


try

{

selectedDate = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", format);

}

catch

{

selectedDate = new DateTime();

}


return selectedDate;

}

set

{

if (value == DateTime.MinValue)

txtDate.Text = "";

else

txtDate.Text = value.ToString("dd/MM/yyyy");

}


public string GregorianDateText

{

get { return txtDate.Text; }

set { txtDate.Text = value; }

}




Dates in your database don't have a format, they represent a point in time.  Dates only have a format when you convert them to a string.  So get the date from the database as a DateTime class (not as a string) and use ToString when you want to display it


DateTime dtMyDate = (DateTime) somedatasource["SomeDateTimeField"]; // don't know how you're getting data from your database, change as needed


dtMyDate.ToString("MM/dd/yyyy");



String MyString = "30-12-2014"; 
DateTime MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "dd-MM-yyyy",null);
String MyString_new = MyDateTime.ToString("MM-dd-yyyy");

沒有留言:

張貼留言