I am getting this error: String was not recognized as a valid DateTime.
DateTime date = DateTime.ParseExact("4/29/2013", "MM/dd/yyyy", null);
Use one M instead of MM to match the format for months values that are one or two numbers.
The same goes for d for days.
DateTime date = DateTime.ParseExact("4/29/2013", "M/dd/yyyy", null);
DateTime aDate = DateTime.ParseExact("4/2/2013", "M/d/yyyy", null);
Related
I want to change the date format
CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
string fromdate =(TxtFrom.Text);
string todate = (TxtTo.Text);
DateTime dt =DateTime.Parse(fromdate);
DateTime d =DateTime.Parse(todate);
_DivAPath.FROM_DATE = Convert.ToDateTime("d",ci);
_DivAPath.TO_DATE = Convert.ToDateTime("d",ci);
But it will show exception that the given Datetime is not a correct format.how to change the datetime function...
please explain
First, you don't need to actually change the culture if you want to parse or convert dates with a given CultureInfo, so this is unnecessary:
Thread.CurrentThread.CurrentCulture = ci;
You can simply use the DateTime.Parse overload that accepts the culture:
DateTime dt = DateTime.Parse(fromdate, ci);
The exception is probably raised at Convert.ToDateTime("d",ci); since d is not a valid date ;)
Maybe FROM_DATE is a string property and you actually want to convert the datetime to a short-date-string, then you could either use:
_DivAPath.FROM_DATE = dt.ToString("d", ci);
or
_DivAPath.FROM_DATE = dt.ToShortDateString(); // uses the current-culture
You are passing the String "d" as a value to convert to DateTime. Pass d and dt without the quotation marks.
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
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();
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...
this is part of my code
//Instantiate the object we have to deal with
string Name = txtName.Text;
string Description = txtDecription.Text;
string Topic = txtTopic.Text;
string Sponsor = txtSponsor.Text;
string Location = txtLocation.Text;
System.DateTime StartDate;
StartDate= DateTime.Parse(txtStartDate.Text);
System.DateTime EndDate = DateTime.Parse(txtEndDate.Text);
string URL = txtURL.Text;
try
{
intResult = pBAL.Insert(Name,Description, Topic,Sponsor, Location,StartDate,EndDate,URL);
}
error:Argumnet 6 and 7 .cannot convert from System .DateTime to 'string
Need some help
Simple Your function expects string parameter you passing datatime as parameter,
Solution 1:
Change this statements
System.DateTime StartDate; StartDate= DateTime.Parse(txtStartDate.Text);
System.DateTime EndDate = DateTime.Parse(txtEndDate.Text);
to
string StartDate= txtStartDate.Text;
string EndDate = txtEndDate.Text;
Solution 2:
or while passing argument convert datetime object to string object
StartDate.ToString()
EndDate.ToString()
That simple,
Try:
intResult = pBAL.Insert(Name,Description, Topic,Sponsor, Location,StartDate.ToString(),EndDate.ToString(),URL);
please check your text box date format it must MM/dd/yy then it will be parse in date time format