Convert a string to datetime format in asp .net - c#

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)

Related

Convert a string has datetime to DateTime

I want to convert a string of date and time to DateTime structure, but it is giving this error :
String was not recognized as a valid DateTime
DateTime dt = Convert.ToDateTime("5/15/2018 11:54:18 AM");
string date= dt.ToString("HH:mm");
I'm reading this question but I can't solve this code. What is my mistake?
What is the difference between Convert.ToDateTimeand DateTime.ParseExact() in C#?
Based on all the comments, here's how your code should look like
DateTime dt = DateTime.ParseExact("05/15/2018 11:54:18 AM", "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
string date = dt.ToString("HH:mm");
What is my mistake?
You Mistake is You are Providing argument Convert.ToDateTime() in a wrong Format.
try Providing "DD/MM/YYYY HH:MM:SS" according to Your system date Time Format .Else you need to use TryParseExatct with Specifying Format

display datetime as yyyy/mm/dd in asp mvc/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")

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

C# with mongodb DateTime Convert

I want to convert "ISODate(\"2014-11-13T18:43:33.868Z\")" to c# datetime ex 2014-11-13 18:43:33.
Value "ISODate(\"2014-11-13T18:43:33.868Z\")" take from MongoDB collection.
Please Help.
You can set DateTime in C# to UTC
var createDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc)
or
dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc)
Then insert type DateTime into mongodb
It worked in my case.
You can store your date as a BsonDateTime object when you pull it from the database, then convert it as follows:
DateTime dt = bdt.ToUniversalTime();
And you may find this question useful to learn more about how ToUniversalTime() works.
If I understand clearly, just because it writes ISODate in your string, that doesn't make it ISO 8601 format. The "O" or "o" standard format specifier complies ISO 8601 format and which is "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for a DateTime. That doesn't match with your string format.
If your all strings has a stable format like this, you can use custom date and time formats with literal string delimiter like;
string s = "ISODate(\"2014-11-13T18:43:33.868Z\")";
string format = "'ISODate(\"'yyyy-MM-dd'T'HH:mm:ss.fff'Z\")'";
DateTime date;
if(DateTime.TryParseExact(s, format, CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
Console.WriteLine (date);
}
If you want to string representation of your DateTime with "2014-11-13 18:43:33" format, you can use DateTime.ToString() method like;
date.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

date conversion error

I am developing windows application.
In that i have date in the string format as>> fileDate="15/03/2013"
I want it to be get converted into date format as my database field is datetime.
I used following things for it>>
DateTime dt = DateTime.ParseExact(fileDate, "yyyyy-DD-MM", CultureInfo.InvariantCulture);
DateTime dt = DateTime.Parse(fileDate);
Both of these methods proved failure giving me error>>
String was not recognized as a valid DateTime.
What can be mistake?
Is there another technique to do that?
string fileDate = "15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
You have to give the date format according to the date string you have to ParseExact. You can see more on Custom DateTime format - MSDN
Change
"yyyy-MM-dd HH:ss"
To
"dd/MM/yyyy"
Your code would be
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You should do this:
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy",CultureInfo.InvariantCulture);
You must pass in the string for the format ("dd/MM/yyyy") in the same style that you pass in the string fileDate.
u may try with this
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse("ur_dateString")
In your current code you are using format "yyyyy-DD-MM" which is wrong since date part require lower case d not upper case D. , Also for year part you are specifying 5 ys, it should be 4, like yyyy, the order according to your date string should be: "dd/MM/yyyy". To be on the safe side you can even use "d/M/yyyy", which would work for single digit or double digit day/month.
So your code should be:
string fileDate="15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You can see more on Custom DateTime format - MSDN
It's because string "15/03/2013" cannot really be parsed as DateTime with format string "yyyy-MM-dd HH:ss".

Categories