Getting valid Datetime such as Month/day/year format - asp.net

my string is like this "21.05.2013 00:00:00"
but i want like this : "05/21/2013";
so i used format
String.Format("{0:M d yy}", myObject.StartAt.ToString());
but instead of this "05/21/2013" it produce this
"05.21.2013 00:00:00"
Edited area ----------
i tried below code but it still give wrong format it still give "05.20.2013" instead of "05/20/2013";
DateTime arrivalDate = DateTime.ParseExact(hotelSearchModel.StartAt.ToString(), "dd.MM.yyyy hh:mm:ss", CultureInfo.InvariantCulture);
DateTime departureDate = DateTime.ParseExact(hotelSearchModel.StartAt.ToString(), "dd.MM.yyyy hh:mm:ss", CultureInfo.InvariantCulture);
request.arrivalDate = arrivalDate.ToString("MM/dd/yyyy");
request.departureDate = departureDate.ToString("MM/dd/yyyy");

Is StartAt a string or a DateTime? If it's a string, you'll need to convert it to DateTime for the formatting to work. Also, you'll need to change your formatting pattern to MM/dd/yyyy to get 05/21/2013:
DateTime dt = DateTime.ParseExact(myObject.StartAt, "dd.MM.yyyy hh:mm:ss", CultureInfo.InvariantCulture);
string s = dt.ToString("MM/dd/yyyy");

Related

String was not recognized as a valid DateTime C# Asp.net

I have date in string format 20/05/2016:
string weekEndDate="20/05/2016"
when convert it to DateTime,An error Occured:
DateTime EndDate = Convert.ToDateTime(weekEndDate);
String was not recognized as a valid DateTime.
Try changing your string format to "yyyy-mm-dd" or "yyyy/mm/dd"
You can also use DateTime.TryParse method to parse date from any date format. See examples
You can use ParseExact method to parse the string into DateTime.
string weekEndDate = "20/05/2016";
DateTime EndDate = DateTime.ParseExact(weekEndDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Or you can use TryParseExact it will not throw exception if string is not parsed into DateTime
DateTime.TryParseExact(weekEndDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out EndDate);
Use TryParseExact
string d1 = "11/18/2016 11:45:44 AM";
string d2 = "11/18/2016 11:45:59 AM";
DateTime frmdate;
DateTime todate;
CultureInfo enUS = new CultureInfo("en-US");
bool f = DateTime.TryParseExact(d1, "M/dd/yyyy HH:mm:ss tt", enUS, DateTimeStyles.None, out frmdate);
bool t = DateTime.TryParseExact(d2, "M/dd/yyyy HH:mm:ss tt", enUS, DateTimeStyles.None, out todate);
TimeSpan val = frmdate - todate;

convert string contains +GMT date to datetime object

how convert string ="yyyyMMddHHmmss+GMT" to datetime
example string date="20130607000000+1000"
i need for that convertor
whats relation by this forme string and datetime
DateTime dt = DateTime.ParseExact(
date, "yyyyMMddHHmmsszzz", null, DateTimeStyles.None);
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

string variable date to double in c#

In the following code ,i need to convert string to double. But the code doesn't work.
string fdate="7/4/2013";
double nextdate = Convert.ToDouble( fdate);
Try this..
DateTime ddd=Convert.ToDateTime("7/4/2013");
double dd = Convert.ToDouble(Convert.ToString(ddd.Month) + Convert.ToString(ddd.Day) + Convert.ToString(ddd.Year));
It will surely work
First convert date string you have to date using DateTime.ParseExact and use it to Convert.ToDouble or DateTime.ToOADate to convert it to double.
string fdate = "7/4/2013";
DateTime date = DateTime.ParseExact(fdate, "d/m/yyyy", System.Globalization.CultureInfo.InvariantCulture);
double nextdate = date.ToOADate();

How to convert date string to specific date format?

I get date string value(3/13/2013 12:00:00AM) from database and i need to convert like this format (yyyy-mm-dd). Please help me solve this.
string targetdate = "3/13/2013 12:00:00AM";(getting date value from DB)
DateTime lastdate = DateTime.ParseExact(targetdate, "yyyy-mm-dd",
System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
And I tried
Iformatprovider = null.
but i getting same error "String was not recognized as a valid DateTime"
I think the problem is with the date time
"3/13/2013 12:00:00AM"
It should not be 12:00:00AM.
It should be 12:00:00PM.
Example
string targetdate = "3/13/2013 11:59:59AM";
DateTime lastdate = DateTime.ParseExact(targetdate,
"M/d/yyyy HH:mm:sstt",
System.Globalization.CultureInfo.InvariantCulture);
lastdate=lastdate.AddSeconds(1);
You will get
3/13/2013 12:00:00 AM
I would suggest you to cast it in the database end.
If you are using sql server then
Example
The following script uses the CONVERT() function to display different formats. We will use the GETDATE() function to get the current date/time:
CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)
The result would look something like this:
Nov 04 2011 11:45 PM
11-04-11
11-04-2011
04 Nov 11
04 Nov 2011
04 Nov 2011 11:45:34:243
First you need to convert your date string to DateTime type object using the format "M/d/yyyy HH:mm:sstt" later you can get the formatted string using "yyyy-MM-dd". (You used lower case m for month, it should be upper case M for month.
string targetdate = "3/13/2013 12:00:00AM";
DateTime lastdate = DateTime.ParseExact(targetdate,
"M/d/yyyy hh:mm:sstt",
System.Globalization.CultureInfo.InvariantCulture);
string newFormat = lastdate.ToString("yyyy-MM-dd");
newFormat would contain "2013-03-13"
DateTime conversion is really easy in .Net if you know which datetime format you have and in which format you convert that.
Here is example for this.
String origionalDate = "12/20/2013"; // Format : MM/dd/yyyy
string origionalFormat = "MM/dd/yyyy";
string convertInToFormat="dd/MM/yyyy";
String convertedDate;
DateTime objDT;
if (DateTime.TryParseExact(origionalDate, origionalFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out objDT) == true)
{
convertedDate = objDT.ToString(convertInToFormat);
Response.Write("<b>Origional DateTime Format ( " + origionalFormat + " ) : </b>" + origionalDate);
Response.Write("<br/>");
Response.Write("<b>Converted DateTime Format ( " + convertInToFormat + " ) : </b>" + convertedDate);
}
else
{
Response.Write("<b>Not able to parse datetime.</b>");
}
For more details on this visit this link. Click Here...

How can I get a datetime from this string '01/25/1955'?

I have the following code:
Dim dtDoM As Nullable(Of DateTime)
If (txtMarriageDate.Text.Trim = "") Then
dtDoM = Nothing
Else
dtDoM = DateTime.ParseExact(txtMarriageDate.Text.Trim + " 00:00:00", "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
'dtDoM = Convert.ToDateTime(txtMarriageDate.Text)
End If
On the commented out section I was getting FormatException 'String not recognized as datetime' and on the new code I'm getting "System.FormatException = {"The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar."}"
EDIT: Answered. Can't believe I overlooked that.
Your date (01/25/1955) is in MM/dd/yyyy format, you're using dd/MM/yyyy in your format string.
Try using this: "MM/dd/yyyy hh:mm:ss"
I think you have the wrong format string. You want "MM/dd/yyyy hh:mm:ss", judging by your examples.
You got day and month reversed. This will work:
dtDoM = DateTime.ParseExact(txtMarriageDate.Text.Trim + " 00:00:00", "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)

Resources