DateTime Converting - c#

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);

Related

Format datetime from string "20151210T11:25:11123"

I have string format "20151210T11:25:11123", can't convert to type DateTime in C# help me?
string date = "20151210T11:25:11123";
DateTime datea = DateTime.ParseExact(date, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
You are using a time of 20151210T11:25:11123 but telling it to parse it as if it were formatted as dd/MM/yyyy hh:mm tt. The format does not match the string, so you get a FormatException. You need to provide a format that matches the string you have. It isn't clear to me what the last 5 digits are but a format like yyyyMMddThh:mm:ssfff will parse the string as 12/10/2015 11:25:11 AM. You may need to adjust the last part of the format to match whatever is actually encoded there in your string.
string date = "20151210T11:25:11123";
DateTime datea = DateTime.ParseExact(date, "yyyyMMddThh:mm:ssfff", CultureInfo.InvariantCulture)
Console.WriteLine(datea); // 12/10/2015 11:25:11 AM
with value datetime string ="20160121T13:26:24090"
code error :
DateTime datea = DateTime.ParseExact(date, "yyyyMMddThh:mm:ssfff", CultureInfo.InvariantCulture)
Console.WriteLine(datea); // 12/10/2015 11:25:11 AM

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");
}

String not getting converted to datetime

I have I have Excel file from where I am getting date in string format as "30-12-1899 07:50:00:AM"
When I am trying to convert it to DATETIME then it is giving error as
String was not recognized as a valid DateTime
I am trying to convert it like this
Convert.ToDateTime(homeToSchool[7],new DateTimeFormatInfo { ShortDatePattern = "dd-MM-yyyy", DateSeparator = "-" })
Use DateTime.ParseExact
string myDate = "30-12-1899 07:50:00:AM";
DateTime dt1 = DateTime.ParseExact(myDate, "dd-MM-yyyy hh:mm:ss:tt",
CultureInfo.InvariantCulture)
For more information about Date and Time Format Strings,
Custom Date and Time Format Strings
Since you are reading this from excel I hope this would help
DateTime.FromOADate(homeToSchool[7].ToString("dd-MMM-yyyy");

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 error in 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/

Categories

Resources