I have date in string format. I want to convert in date time in following format. I have referred few links but didn't get exact output.
String date = "03-23-16"; //MM-dd-yy
Requirement: it should be in date format like "March 23, 2016"
Can anybody suggest me how to convert this?
You could convert it to DateTime by using, say, DateTime.ParseExact and then convert it back to string using format "MMMM dd, yyyy":
String date = "03-23-16"; //MM-dd-yy note that MM here
DateTime dtInstance = DateTime.ParseExact(date, "MM-dd-yy", null); //this is how you convert string to DateTime
string newDate = dtInstance.ToString("MMMM dd, yyyy"); //this is how you convert it back with format as you want
Also, note that mm is minutes format in C# DateTime while MM is months.
DateTime.ParseExact("03-23-16", "MM-dd-yy", null).ToString("MMMM dd, yyyy")
You could convert it to Long Date Format by using ToLOngDateString()
string date = "03-05-16";
date = date.Replace('-', '/');
DateTime dt = Convert.ToDateTime(date);
string newDate = dt.ToLongDateString();
it will print :March, 03, 2016.
try this:
string date = "01-08-2008";
DateTime dt = DateTime.ParseExact("24/01/2013", "mm-dd-yy", CultureInfo.InvariantCulture);
parse to string:
dt.ToString("MMMM dd, yyyy");
Related
Can someone please let me know how do I convert this datetime format into yyyyMMdd
2/28/2017 12:02:04 AM
At the output I should get 20170228
Any advice on this?
If you already have the DateTime as an object
string formattedDate = date.ToString("yyyyMMdd");
If you need to parse the value first.
string dateValue = "2/28/2017 12:02:04 AM";
string format = "M/d/yyyy hh:mm:ss tt";
DateTime dateTime = DateTime.ParseExact(dateValue, format,
System.Globalization.CultureInfo.InvariantCulture);
For reference you can find a breakdown of the Custom Date and Time Format Strings
You need to specify the format of the date.
If you want it for the current time you can try like this :
string dtime = DateTime.Now.ToString("yyyy/MM/dd");
This is the solution I have come up with for you:
string format = "M/d/yyyy hh:mm:ss tt";
string dateString = "2/28/2017 12:02:04 AM";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime date = DateTime.ParseExact(dateString, format, provider);
string output = date.ToString("yyyyMMdd");
If you're using C# 6 or later (VS2015), you can format DateTime objects easily by using string interpolation using a custom format string. The custom format string that you're looking for is "yyyyMMdd".
// create your preferred date and time in a new DateTime struct
DateTime yourDateTime = new DateTime(2017, 2, 28, 0, 2, 4);
// format yourDateTime as a string
string yourFormattedDateTime = $"{yourDateTime:yyyyMMdd}";
You can read more about interpolated strings at https://msdn.microsoft.com/en-us/library/dn961160.aspx, and, as previously mentioned by #Adam Carr, you can find more information on custom date and time format strings at https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
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);
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
I want to Get date-time Format like (30 April 1990) my date is store in sql server Database
in default Format mm/dd/yyyy.
how to do that?
tsddate.Text = orderReader["deliveryDate"] as DateTime = new DateTime(?,,????);
You can specify custom datetime formats using the ToString overload.
like:
DateTime now = DateTime.Now;
string formatted = now.ToString("dd-MM-yyyy");
And in your situation it would be something like:
DateTime date = DateTime.ParseExact(orderReader["deliveryDate"].ToString(), "MM/dd/yyyy HH:mm:ss", new System.Globalization.CultureInfo("en-US"));
tsddate.Text = date.ToString("dd MMMM yyyy");
see all the formats you can use here:
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
tsddate.Text = String.Format("{0:dd MMMM yyyy}",orderReader["deliveryDate"]);
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/