views:

128

answers:

6

How do I process a date that is input by the user and store it in the database in a date field? The date is entered by the user using a jquery datepicker in a text field.

Here are specific needs I have:
1. How do you convert a mm/dd/yy string (the jquery picked can produce any format) to a format storable in the database as a date field.
2. How to get the date and then change it to something like "Wednesday, Aug 11, 2009"?

I'm using C# for the backend, but I should be able to understand VB Code as well.

Thanks!

+7  A: 
string dateString = "08/11/09";
DateTime yourDate;

if(DateTime.TryParse(dateString, out yourDate)) 
{
   // do something with yourDate
   string output = yourDate.ToString("D"); // sets output to: Tuesday, August 11, 2009
}
else 
{
   // invalid date entered
}

Here is a list of DateTime format strings: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

John Rasch
Thanks for the example! I slowly understood that it answered both parts after seeing Russ's answer.
stringo0
+1  A: 

cDate(string) will give you a actual date from a string, and dateformat(string,option) will return a string formatted as the option states - longdateformat is one of the options that I think you're looking for.

NickAtuShip
+1  A: 

use DateTime.Parse(string) to parse the string to a DateTime which can then be saved as a DateTime in the database. You may want to use TryParse as suggested in other answers, to ensure that the supplied string can be parsed to a DateTime.

To return a DateTime in a certain format, you can call .ToString() on the DateTime and supply a format or use one of the pre-defined formats e.g. ToShortDateString()

Russ Cam
Thanks - this explained it really quickly to me!
stringo0
+1  A: 

The previous answers will check to see that it is a date (although i do a try catch block to do it) Once you have confirmed the string to be a date, you can just pass it to a stored procedure or put it in single quotes and make it part of your insert/update statement:

string yourdateString ="5-5-09";

update table set da='" + yourDateString + "' WHERE id=@id
chad
+2  A: 

I agree with John Rasch's solution. What people often don't know is that you can also use the String.Format(...) method to format your date (and other data types). For dates it is obviously more convenient to use the ToString() method, since the date provides such functionality, but other data types will probably return you the object's address in the ToString().

So given you get your date from the DB or somewhere as DateTime object you can do the following:

DateTime date = DateTime.Now; //normally would come from somewhere else

//Wednesday, Aug 11, 2009 <-- what we want to get displayed
string displayFormat ="dddd, MMM dd, yyyy";
string formattedDate = string.Format("{0:" + displayFormat + "}", date);

Response.Write(formattedDate);

This will exactly print what you requested. The explanation of the different format strings can be found here.

Juri
Thanks for the tip that it works mainly for the date data type.
stringo0
+1  A: 

No matter which format you parsed your DateTime field

DateTime dt = DateTime.Now;//*example
//FormatString for "Wednesday, Aug 11, 2009" is *dddd,MMM dd,yyyy*
string formattedString = dt.ToString("dddd,MMM dd,yyyy");

You can globally hold this format and use it

Myra