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
Related
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;
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
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);
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();