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();
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
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");
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
I have a dropdownlist that displays time. For example 8:00AM or 8:30AM.
When I save this time to database, I want to save as todays date + time.
eg: 8:00AM as 03/30/2009 8:00:00:000. Can anybody give appropriate code to convert as shown above?
I tried
Convert.ToDateTime(ddlStartTime.SelectedItem.Text)
But there is an error stating "String was not recognized as a valid DateTime."
VB.NET answer for Portmans Solution. Too many chars for comment so included here.
Dim time As String() = Me.DropDownList1.SelectedValue.Split(New Char() {":", " "})
Dim hours As Integer = Integer.Parse(time(0))
Dim minutes As Integer = Integer.Parse(time(1))
Dim ampm As Integer = 12
If time(2).ToLower() = "am" Then
ampm = 0
End If
Dim dt As DateTime = DateTime.Today.AddHours(hours + ampm).AddMinutes(minutes)
Have a look at DateTime.TryParse and DateTime.Today. Using them should be enough to do what you want.
Untested Code.
DateTime dt;
if (DateTime.TryParse(Dropdown1.SelectedValue, out dt))
{
DateTime result = DateTime.Today.AddHours(dt.Hour).AddMinutes(dt.Minute);
}
Basically, you just need to parse the time string. It will be automatically resolved to the current date.
Dim strTime As String = "8.30am"
Dim parsedTime As DateTime
If DateTime.TryParseExact(strTime, "h.mmtt", New System.Globalization.DateTimeFormatInfo(), Globalization.DateTimeStyles.None, parsedTime) = True Then
'Parse was successful.
Else
'Handle the error.
End If
Store as the value for each drop down item the number of minutes from midnight that the time represents. Then:-
valueToStore = DateTime.Today + TimeSpan.FromMinutes(Int32.Parse(value))
By storing using the value attribute of a HTML option to store a simple representation of the value you eliminate the codes dependancy on the actual format used to simply display the set of values. If it decided that the representation of the times be changed to use different format the rest of the code will continue to work unmodified.
var time = this.DropDownList1.SelectedValue.Split(':', ' ');
var hours = Int32.Parse(time[0]);
var minutes = Int32.Parse(time[1]);
var ampm = (time[2] == "PM") ? 12 : 0;
var dt = DateTime.Today.AddHours(hours + ampm).AddMinutes(minutes);
Parse your DropDownList for Hours and Minutes, then add them to DateTime.Today.
Read the parts of the time so you have hours and minutes, then use the following code:
DateTime myDate = DateTime.Now.Date;
myDate = myDate.AddHours(hours);
myDate = myDate.AddMinutes(minutes);
Use ParseExact so that you can specify the format that you are using.
h = hours in 12-hour clock format
. = literal character
mm = minutes as two digits
tt = AM/PM designator
Dim time As DateTime = DateTime.ParseExact(dropdown.SelectedValue, "h.mmtt", CultureInfo.InvariantCulture)
The components of the DateTime value that you don't specify in the string (year, month, date, seconds, time zone, era) uses DateTime.Today as default, which is exactly what you want in this case.
This always seems to work for me. May not be the most elegant way, but I have not had any issues so far.
dTime = DateTime.Now;
string time = this.DropDownList1.SelectedValue;
DateTime FormattedDateTime = Convert.ToDateTime(dTime.Date.ToShortDateString() +
" " + time);