Textbox textmode is DateTimeLocal - c#

I have a textbox and i would love it to be formatted. fortunately for me, i can get this done by changing the textmode = DateTimeLocal. Exactly what I want. Additionally, I would like to load this textbox with default values rather than leaving it with dd/mm/yy __:__:__ . I can change the text if it is a regular textbox (single mode) or even a datetime textbox. but i cannot change it with DateTimeLocal mode. some help please. thank you.

You will have to format the DateTime to a valid Date and Time string that can be parsed, here is one that works:
txtDateTimeLocal.Text = DateTime.Now.ToLocalTime().ToString("yyyy-MM-ddTHH:mm");
For more details on what other attributes you can set, see: http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#local-date-and-time-state-(type=datetime-local)

Try setting the whole datetime format including seconds and milliseconds, worked for me.
txtStartTime.Text = DateTime.Today.ToString("yyyy-MM-ddTHH:mm:ss.ss");

If you want to set a TextBox's DateTime dynamically from a database then you can use this code:
DataTable dtTemp = (DataTable)ViewState["DataSet"]; // this are temporary table
TextBox txtFromDate = (TextBox)gridEditBloackHomework.Rows[e.NewEditIndex].FindControl("txtFromDate");
TextBox txtToDate = (TextBox)gridEditBloackHomework.Rows[e.NewEditIndex].FindControl("txtToDate");
string c = dtTemp.Rows[e.NewEditIndex]["FromDate"].ToString();
DateTime FromDate = Convert.ToDateTime(dtTemp.Rows[e.NewEditIndex]["FromDate"]);
DateTime ToDate = Convert.ToDateTime(dtTemp.Rows[e.NewEditIndex]["ToDate"]);
DateTime dtFromDate = FromDate.AddHours(-2);
DateTime dtToDate = ToDate.AddHours(-2);
txtFromDate.Text = dtFromDate.ToLocalTime().ToString("yyyy-MM-ddTHH:mm").ToString();
txtToDate.Text = dtToDate.ToLocalTime().ToString("yyyy-MM-ddTHH:mm");

Related

I need only date value part and time value part using ASP.NET C#

I have two parameters one for date and another for time, and i need date value part and time values part.
My two parameters are below.
// For Date parameter
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
bo.Dateused5 = dt;
// For Time parameter
string Fromtiming = ddl_FromHours.SelectedItem.ToString() + ":" + ddl_FromMinutes.SelectedItem.ToString();
DateTime InterviewTime = Convert.ToDateTime(Fromtiming);//StartTime
bo.Dateused4 = InterviewTime;//InterviewTime
so i need to send mail to the candidate to only date part, should not contain time and time part, should not contain date.
are you looking for this:
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
string mailDate = dt.ToString("dd-MMM-yyyy");// will give 01-jan-1999
string date = dt.ToString("dd-MM-yyyy"); // will give 01-01-1999
You can also try using String.Format()
string mailDate = String.Format("{0:dd-MM-yyyy}", dt); // will give 01-01-1999
You can use ToShortDateString():
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
var date = dt.ToShortDateString();
Note that it uses date format attached to the current thread's culture info.
You would need to use strings rather than dates, so change the type of your variables to string so that
bo.Dateused5 = dt.ToString("dd-MMM-yyyy")
would set Dateused5 to a string of the date component, then
bo.Dateused4 = InterviewTime.ToString("HH:MM");
would set Dateused4 to the time component.
Couldn't test your code but I am very sure there are Functions "DateValue" and "TimeValue" you can make use of.
Something like,
Format(DateValue(any datetime), "dd-MM-yyyy")
gives you Only Date in the specified format. Similar way for TimeValue

How to get textbox date formatted as a string?

I am using a textbox with text mode set as date. What I need is to arrange the date format to MM/dd/yyyy. I know if I use DateTime.Now, it gets today's date, but I cannot get the value of user's picked date if I do like this as there is error:
string dateOfBirth = DateTime.ToString("MM/dd/yyyy");
This is how I do:
string tmpDateOfBirth = tb_date.Text;
//This below line returns todays date and not the date that the user pick on the calendar.
string dateOfBirth = DateTime.Now.ToString("MM/dd/yyyy");
You are using today's date and formatting it... You need to format what has been entered in your textbox.
string dateOfBirth = tb_date.Text.ToString("MM/dd/yyyy");
EDIT : if you are getting 'best overloaded match error'
string dateOfBirth = Convert.ToDateTime(tb_date.Text).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Okay, here i assume your TextBox1.Text contain valid date value. So below code might help you.
string dateOfBirth = Convert.ToDateTime(TextBox1.Text).ToString("MM/dd/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
You can directly get the selected value of DateTimePicker.
Try this:
string datetime = DateTimePicker1.Value.ToString("DateTimeFormat");
See this link http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.value%28v=vs.110%29.aspx
How is the user picking the date?
If you have a DateTimePicker object, you can just type
string dateOfBirth = MyDateTimePicker.Value.ToString("MM/dd/yyyy");
EDIT
Textboxes are not meant to hold datetime objects. If you need to do so, you'll have to parse its text:
DateTime dt = DateTime.ParseExact("TextBox1.Text", "MM/dd/yyyy",
CultureInfo.InvariantCulture);

Trouble bringing in a string with d-MMM-yy format parse.exact to date but keeping date format and date type for sorting

As the title said I am bringing in a date string from a datatable with the format of "d-MMM-yy" or 27-AUG-06.
I need to convert it to a date type for sorting, but I need to keep the same format for display.
NOTE: I am using C#, .Net 2.0, and I am retyping this code so bear with me on typos
System.Globalization.DateTimeFormatInfo dtfi;
dtfi = new System.Globalization.DateTimeFormatInfo();
dtfi.ShortDatePattern = "d-MMM-yy";
dtfi.DateSeperator = "-";
//this is in a for loop with rowCnt being the row index/counter: loop and datatable is working fine.
//"newRow" represents a DataRow in the new table.
// the table [row] [column] is bringing in the string date like "27-AUG-06"
//colXDate IS RECORDED AS {8/27/2006 12:00:00 AM}
DateTime colXDate = DateTime.ParseExact(inputDataTable.Rows[rowCnt]["colX"].ToString(), "d-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);
//#### THIS NEXT LINE IS WHERE IT GIVES ME AN ERROR "String was not recognized as valid datetime."
newRow["colX"] = Convert.ToDateTime(colXDate.ToString(), dtfi);
Since you already have colXDate as a DateTime, you don't need to convert it to a string and then back into a datetime.
Instead, try this:
newRow["colX"] = colXDate.ToString("d-MMM-yy");
Your line fails because ToString() will output the date in the format defined in the current culture and you are trying to convert it back to a date using your custom date format.
You need to set it to colXDate.ToString("d-MMM-yy").
SCENARIO: I am passing a datatable back as a DataGrids datasource. I am passing in a string field that is a date. I want to convert it to a date so that I can sort by it but I also want to maintain the same format that it was on the string that came in.
ISSUE: From some of my research it seems that the DateTime type is exactly that a datatype in whatever datatable that you put it in and is not formatable. So even though I bring in an unormal string date and convert it to a datetime via the DateTime.ParseExact, when I put it into my datetime field and try to format it (newRow["colX"] = colXDate.ToString("d-MMM-yy"); //as Scott had above) it still goes in as a set datetime format with hours...etc.
SOLUTION: So I resolved this by smoke and mirrors. I put 2 columns in the datatable, the first being the string format display date and the second the datetime type. On ItemDataBound I hid the datetime column (e.Item.Cells[5].Visible = false;) and then on the sort event I test for string date column (e.SortExpression == "colX") and if it is true I sort by the hidden datetime column.

swicthing format on dates using the datetimepicker

I'm using the datetimepicker to get two dates. They are coming out like this: 24-11-2011..
I want to have them like this: 2011-11-24..
I've tried to accomplish this by doing this method, but its only changes the datetimepicker on the interface..
DateTimePicker1.Format = DateTimePickerFormat.Custom;
DateTimePicker1.CustomFormat = "yyyy-MM-dd";
This is my code. The output says 24-11-2011. I want it to say 2011-11-24.
string sta = dateTimePicker1.Value.ToShortDateString();
string en = dateTimePicker2.Value.ToShortDateString();
Console.WriteLine(sta);
ctrscan.getListOfDates(sta, en);
How can I do this?
thanks
The Value property of the DateTimePicker control will return DateTime type, not string.
To format this have such code:
string myDate = DateTimePicker1.Value.ToString("yyyy-MM-dd");
To change it "globally" change the default in the machine Regional Settings.
Change your System Date Format then only it will display correctly using Console.WriteLine(sta);

How to change the datetimepicker value automatically while in progress

I want to know is there any way to change the datetimpePicker value while in progress.
I am using this code to go the next day .
DateTime stdate = new DateTime();
stdate = dateTimePicker1.Value;
while (stdate <= DateTime.Now)
{
txtSelectedDate.Text = stdate.ToString("yyyyMMdd");
selectedDate = Convert.ToInt32(txtSelectedDate.Text);
stdate = stdate.AddDays(1);
}
I want to change the datetimepicker value while in progress automatically because my my code works on date value if i select the date which is not present in the files it does not works , if i change the datetimepicker value which the file haves it works fine.
I want to change the datetimepicker value while in progress automatically.
There would be great appreciation if someone coulde help me.
Thanks In Advance.
I'm not sure if I've understood your question but if you're looking to update the DateTimePicker after each increment, you just need to affect its Value property, something like this:
stdate = stdate.AddDays(1);
dateTimePicker1.Value = stdate;

Categories