display datetime as yyyy/mm/dd in asp mvc/c# - c#

I use the following code to convert an datetimestring to datetime.
string str1 = "1392/02/10 22:30:15";
DateTime d = DateTime.ParseExact(str1, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
I have a problem that when it convert to datetime I need to display
1392/02/10 22:30:15 I mean as a yyyy/mm/dd but it display 02/10/1392 22:30:15 as mm/dd/yyyy.

The format specifier you pass to ParseExact tells C# how to convert a string into a DateTime, but you want to convert DateTime to a string.
You can pass a custom format to the DateTime.ToString() method.
string display = d.ToString("yyyy/MM/dd HH:mm:ss");

Convert date to string before displaying.
string strDate = d.ToString("yyyy/MM/dd HH:mm:ss")

Related

How to convert dd/MM/YYYY formatted string date to YYYY-MM-dd datetime? [duplicate]

This question already has answers here:
Converting string format to datetime in mm/dd/yyyy
(6 answers)
Closed 6 years ago.
I wanna convert dd/MM/YYYY formatted string date to YYYY-MM-dd datetime. But it returns to me
"String was not recognized as a valid DateTime."
How can i convert from "04/26/2016" string to yyyy-MM-dd datetime format?
DateTime dt = DateTime.ParseExact("04/26/2016", "yyyy-MM-dd", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
You parse the string with date in wrong way.
You should:
DateTime dt = DateTime.ParseExact("04/26/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("yyyy-MM-dd"));
Technically, you can do just some string manipulations:
String source = "04/26/2016";
String result = String.Join("-", source.Split('/').Reverse());
But, DateTime.ParseExact is a better solution:
String result = DateTime
.ParseExact(source, "MM/dd/yyyy", CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Clearly, your format and string does not exactly match. From documentation;
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
You should use MM/dd/yyyy format instead.
DateTime dt = DateTime.ParseExact("04/26/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);
And if you wanna get it's string representation with yyyy-MM-dd format, just use ToString method like;
Console.WriteLine(dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
Be aware, there is no YYYY as a custom date format. Since those specifiers are case sensitive, you should use yyyy format specifier instead.
Try this way
DateTime dt = DateTime.ParseExact("04/26/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("yyyy-MM-dd"));

How to convert a date string from "dd-MMM-yy" to "M/dd/yyyy hh:mm:ss tt"?

I need to compare two date format strings:
dateString in "dd-MMM-yy" format
with
referenceDateString in "M/dd/yyyy hh:mm:ss tt" format respectively.
For that, I need to convert the dateString = "dd-MMM-yy" to "M/dd/yyyy hh:mm:ss tt".
However, Got an error while trying to do that:
"Error: string was not recognized as a valid datetime".
The C# code I used given below.
string dateString = "19-Dec-14";
string AsofDate = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Edit 1:
In the actual code the dateString obtaining after reading a csv file which is supplied as "19-Dec-14", that's why it's in the string format.
Please help, am pretty new to C#. Thanks.
Habib already gave the answer on his comments, I try to add it as an answer;
From DateTime.ParseExact(String, String, IFormatProvider)
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
In your case, clearly they don't. First, you need to parse your string to DateTime with proper format (which is dd-MMM-yy with an english-based culture), then you can get the string represention of your DateTime with specific format.
string s = "19-Dec-14";
DateTime dt;
if(DateTime.TryParseExact(s, "dd-MMM-yy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).Dump();
// Result will be 12/19/2014 12:00:00 AM
}
It's not entirely clear what you are trying to do, but in order to parse that date you have on the first line, you would use something like this:
DateTime AsofDate = DateTime.ParseExact(dateString, "dd-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);
Note a couple things here: I've changed the data type of AsofDate from string to DateTime because that's what DateTime.ParseExact returns. Also, I've modified the custom format string to match the format of the string you are trying to parse as a date ("19-Dec-14").

Convert a string to datetime format in asp .net

I have a date time string "12-24-2013 15:19:29" which is in "MM-dd-yyyy HH:mm:ss". I want to convert this string to datetime. But the format should not change.ie, it should be the same "MM-dd-yyyy HH:mm:ss" format.
When I used following method,
DateTime dt2 = DateTime.ParseExact(date31strin, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
the format is changed to "dd-MM-yyyy HH:mm:ss". I have tried some other method also, but still getting this same format.
First, you should be parsing the string to a DateTime and then format it to the second format using ToString() method.
//Convert your string to a DateTime
DateTime dt2 = DateTime.ParseExact(date31strin,
"MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
//If you want to change the format
string secondFormat = dt2.ToString("Second format string");
Note: Date is a Date and it does not have a format. If you need to convert the string to a DateTime, first line of code is enough
You are not changing the format.
You are parsing a string into a DateTime object which does not have a format.
When you decide to present the DateTime, you can format it any way you wish.
to parse String to DateTime use method DateTime.TryParse (http://msdn.microsoft.com/cs-cz/library/ch92fbc1%28v=vs.110%29.aspx) and then use DateTime formating (http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx) to output to aspx page
//frndzz if you are using j query and you don't convert date in(MM/dd/yyyy) to //(dd/MM/yyyy) then this code will help you
// i try it and i get the answer
string sd=txtmydate.Text //sd=04/27/2015 (MM-dd-yyyy) format
string [] sdate = sd.Split('-');
string fd = sdate[1];
fd=string.Concat(sdate[1],"-",sdate[0],"-",sdate[2]);
DateTime dt = Convert.ToDateTime(fd);
txtshow.Text = dt.ToString("dd/MM/yyyy");
//txtshow will show you date as 27-04-2015 (dd/MM/yyyy) format
thanks
How to convert string to DateTime:
string iDate = "Absent";
aEmployeeAttendence.Intime = DateTime.Parse(iDate);
//aEmployeeAttendence (object)
//Intime (field)

C# date time format issue need data dd/mm/yy hh:mm

I want to display date in this format dd/mm/yy hh:mm
HrCreatedDate = DateTime.ParseExact(objMessagePoco.CreatedDate.ToString(), "dd/mm/yyyy hh:mm", CultureInfo.InvariantCulture);
But it's giving me an error:
String was not recognized as a valid DateTime.
Why is that?
DateTime.ParseExact parses a string into a DateTime. If you just want to format an existing DateTime as a string, just pass the format to ToString:
var myStr = objMessagePoco.CreatedDate.ToString("dd/MM/yy HH:mm");
I believe the issue was you needed uppercase MM for month.

Trying to parse String to DateTime into format "02/01/2010" dd/MM/yyyy rather than "1/2/2010 MM/dd/yyyy

I am trying to convert a string into date time format.
DateTime.Parse(tempfrmBankDetails.dgvBankDetails.SelectedRows[0].Cells["PaymentDate"].Value.ToString(),null);
This is printed as an output but i want it in dd/MM/yyyy format. How should i parse it
{1/2/2010 12:00:00 AM}
EDIT
I have created a custom control which accepts string input and their i am using mask as "00/00/2\000".
Here i have done all the validation into my control so as to accept all valid date but they should be only in format dd/MM/yyyy. That's why i want to convert it into string
Try this:
string stringValue = tempfrmBankDetails.dgvBankDetails.SelectedRows[0]
.Cells["PaymentDate"].Value.ToString();
DateTime dateValue = DateTime.ParseExact(stringValue, "M/d/yyyy");
// use this when you need to show that formatted date value
string formattedDate = dateValue.ToString("dd/MM/yyyy");
DateTime.ParseExact

Categories