DateTime error in C# - c#

When I enter the date in the below format then my db accepts it
22/06/2011 00:00:00
But when I enter the date in this format
mm/dd/yyyy 00:00:00
then my DB throws an error saying DateTime not recognised. My calendar gives me DateTime in mm/dd/yyyy hh:mm:ss format. How can I change that in my code to dd/mm/yyyy?
DateTime Res_date = Convert.ToDateTime(txt_DT.Text);
param[5] = new MySqlParameter("#RespondBy", MySqlDbType.DateTime);
param[5].Value = Res_date;
command.Parameters.AddWithValue("#RespondBy", Res_date);
The DateTime is entered in the txt_DT.text textbox.
How can I convert the date and then convert the string to DateTime?

I think you should use the DateTime.ParseExact method. Msdn Documentation http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
DateTime Res_date = DateTime.ParseExact(txt_DT.Text, "MM/dd/yy hh:mm:ss", ... );
Then you can use String.Format to convert your DateTime to any format you want.
http://www.csharp-examples.net/string-format-datetime/

Related

convert "23/11/2016" date into "MM/dd/yyyy" format in C#

string valid= "22/11/2016";
DateTime date = DateTime.ParseExact(valid, "MM/dd/yyyy", null);//not valid date time format
when i am trying to convert date in "MM/dd/yy" format it gives error that inputted string not in valid date time format.Please help
format should be "dd/MM/yyyy",note that d for date and M for month, 22 can't be a month
string valid= "22/11/2016";
DateTime date = DateTime.ParseExact(valid, "dd/MM/yyyy", null);//
You can try this
string valid = "22/11/2016"; // should be in this format dd/MM/yyyy
DateTime date = Convert.ToDateTime(valid);

convert to stamp time format from string c#

I use javascript to pick a date and display the format as (11-05-2015 17:37)
and I try to parse it to date time as the code below
DateTime taskDate = Convert.ToDateTime(txtDate.Text);
and save it into my date base like
TO_DATE('" + createOn + "')
its give me and error call "String was not recognized as a valid DateTime."
anyone have any others method to parse it to stamptime ?
the txtDate.Text value is 27-05-2015 09:37.
Convert.ToDateTime uses your current thread culture format when converting from string to datetime.
If string you converting from has another format, you need to use DateTime.ParseExact and explicitly provide appropriate format.
For example, in you case it should be
DateTime taskDate = DateTime.ParseExact("11-05-2015 17:37", "dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture);
Also have a look to custom datetime format strings for your reference.
You can use DateTime.ParseExact or DateTime.TryParseExact
Check below code:
DateTime taskDate = DateTime.ParseExact(txtDate.Text, "dd-MM-yyyy hh:mm", CultureInfo.InvariantCulture);
Or
DateTime taskDate;
if (DateTime.TryParseExact(txtDate.Text, "dd-MM-yyyy hh:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out taskDate))
{
//code if date valid
}
else
{
//code if date invalid
}

Converting String to DateTime in C# in a particular format

How can I convert a string such as 07/26/13 into a C# DateTime variable in the following format? 2013-07-26 00:00:00 (Mysql datetime)
If your CurrentCulture has MM/dd/yy as a standard date and time format, you can just use DateTime.Parse method like;
DateTime date = DateTime.Parse("07/26/13");
If it is not, you can use DateTime.ParseExact or DateTime.TryParseExact methods to parse your string with custom date and time format like;
string s = "07/26/13";
DateTime date;
if(DateTime.TryParseExact(s, "MM/dd/yy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
Console.WriteLine(date);
}
DateTime doesn't have any implicit format. It has just date and time values. String representations of it have formats. You can format your DateTime with .ToString method like;
date.ToString("yyyy-MM-dd HH:mm:ss");
And by the way, if you want to insert a DateTime value to your database, you shouldn't insert it as a string. You should use parameterized queries and pass your DateTime value to directly to your parameter.
Use TryParseExact.Before posting a question try it yourself
string date_time = "07/26/13";
DateTime d;
if (DateTime.TryParseExact(date_time, "mm/dd/yy", new CultureInfo("en-US"),
DateTimeStyles.None,
out d))
{
date_time = d.ToString("yyyy-mm-dd hh:mm:ss");
}

Issue in Converting Date Time c#

I am facing an issue when converting the datetime
var date = DateTime.Now;
txtdate.Text = date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
//I need to pass it later to as a DateTime variable. When i re-convert it gives me an error)
DateTime dtReconvert =Convert.toDateTime(txtdate.Text); //Error String was not recognized as a valid DateTim
When i set the datetime to something like "01/01/2013"
and convert it to Date Time it not giving me any error.
Use DateTime.ParseExact with the format "dd/MM/yyyy"
DateTime dtObject = DateTime.ParseExact(txtdate.Text,
"dd/MM/yyyy",
CultureInfo.InvariantCulture);
try this
DateTime.ParseExact(txtdate.Text, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
or
Convert.ToDateTime(txtdate.Text, CultureInfo.InvariantCulture)
Microsoft have adopted MM/dd/yyyy as a culture-agnostic format, it's an ambiguous format which isn't something that I would want to build a large system on.

DateTime Converting

I am trying to convert a string into a DateTime and change its format from 05/06/2012 12:00:00 to 2012-06-05 12:00:00.000 to search in the database (SQL Server 2008 R2) DATETIME column type. The original date is coming from a calendar!
I tried this:
string Datereturn = row.Cells[9].Text;
DateTime dategiven = DateTime.ParseExact(Dategiven,
"YYYY-MM-DD hh:mm:ss[.nnn]", CultureInfo.InvariantCulture);
but it pops an error of invalid datetime
To parse 05/06/2012 12:00:00:
DateTime dategiven = DateTime.ParseExact(Dategiven,
"dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
To get a different formatted string:
string newFormat = dateGive.ToString("yyyy-MM-dd hh:mm:ss");
I suggest reading Custom Date and Time Format Strings on MSDN.
Also the different between parsing and formatting (in regards to DateTime):
Parsing means taking a string representing a datetime and getting a DateTime object from it
Formatting a DateTime is taking a DateTime object and getting a string from it, formatted as needed.
Can you try this
DateTime dategiven = DateTime.ParseExact(Dategiven, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);

Categories

Resources