How to use culture for changing the datetime format - asp.net

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.

Related

String was not recognized as a valid DateTime

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

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 insert DateTime column in textbox

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

Converting date to day of week

is there any ready to go solution within the microsoft framework, regarding conversion of date to day?
For example, i would like to convert this string 21/03/2010 (dd/mm/yyyy) to Sunday
Dim d = DateTime.Parse("21/03/2010").DayOfWeek()
This code will print Sunday on the console window
Dim dateToShow as DateTime = new DateTime(2010, 03,21)
Console.WriteLine(dateToShow.DayOfWeek.ToString)
This should print "Sunday".
string myDateTimeString = "21/03/2010";
DateTime dt = DateTime.ParseExact(
myDateTimeString, "dd/MM/yyyy",
new CultureInfo("en-Us", true)
, DateTimeStyles.NoCurrentDateDefault);
Console.WriteLine(dt.DayOfWeek);
I would use DateTime.TryParse() just to validate the user input.
Dim input As String = "2010/12/23"
Dim dateTime As DateTime
If DateTime.TryParse(input, dateTime) Then
Console.WriteLine(dateTime.DayOfWeek)
Else
Console.WriteLine("Invalid")
End If

How to convert to datetime

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

Resources