customizing datetimepicker - c#

I'm making a program using C# vs2008, this is a preparation for my thesis. I'm trying to make my date time picker to show month/day/year without the time in my datagridview. My date time picker is set into datetime.
Here's the sample of my code:
Registrar.InsertStudent(
Convert.ToInt32(txtStud_ID.Text),
txtFname.Text,
txtGname.Text,
txtMname.Text,
txtPAdd.Text,
txtLline.Text,
txtMobile.Text,
cbCitizen.SelectedItem.ToString(),
dtpDOB.Value,
Convert.ToInt32(txtAge.Text),
cbGradeLvL.SelectedItem.ToString(),
cbGender.SelectedItem.ToString(),
txtMotherName.Text,
txtMComp.Text,
txtMOcc.Text,
txtMAdd.Text,
txtMTele.Text,
txtFatherName.Text,
txtFComp.Text,
txtFOcc.Text,
txtFAdd.Text,
txtFTele.Text,
Convert.ToInt32(txtSiblings.Text),
txtSchool_Last_Attend.Text,
txtSchool_Add.Text,
txtDname.Text,
txtDTele.Text,
txtInsuComp.Text,
cbMedical_List.SelectedItem.ToString(),
txtPerson_Notified.Text,
cbRelation_Pupil.SelectedItem.ToString(),
txtIE_Contact.Text,
txtOther_Allergy.Text,
txtOtherIE.Text
);

You can set custom date format to the DateTimePicker as follows:
this.datetimepicker.Format = DateTimePickerFormat.Custom;
this.datetimepicker.CustomFormat = "MM-dd-yyyy"

You can try with this code
var result = dateTimePicker1.Value.Date.ToString();

Don't fully understand your question but if you can access the DateTime variable.
DateTime dt = DateTime.Now;
string dateString = dt.ToString("yyyy-mm-dd");
dateString now equals = "2012-09-08" (depending on timezone)

Related

How can I change time portion of DataTimePicker based on a variable?

I have a DateTimePicker1 in this format "HH:mm:ss dd-MM-yyyy"
How can I save the value (Time) of mytime into the time portion of DateTimePicker1? I only want to change the time portion in DateTimePicker1 and keep the same data.
DateTime mytime = Convert.ToDateTime( "14:53:00");
Thank you
Try following
var mytime =TimeSpan.Parse("14:53:00");
var dp = new DateTimePicker();
dp.Value = dp.Value.Date;
dp.Value.Add(mytime)
.net has special classes for date and time operations - DateTime and TimeSpan. let them do their job like this:
var ts = TimeSpan.Parse("14:53:00");
DateTimePicker1.Value = DateTimePicker1.Value.Date.Add(ts));
You can do this.
yourDateTimePicker.Value = yourDateTimePicker.Value.Date + mytime;

Why date formats miss matching when adding months

When i am getting current date 28-Mar-2015 formated into txtActDate then adding months to it then i am not understanding why its getting in this 28-Mar-15 format.
DateTime dateTime = DateTime.UtcNow.Date;
txtActDate.Text = dateTime.ToString("dd/MMM/yyyy");
DateTime firstDate = DateTime.ParseExact(txtActDate.Text, "dd/MMM/yyyy", null);
firstDate = firstDate.AddMonths(0);
txtAccExp.Text = firstDate.ToShortDateString();
It's almost certainly because you're asking it to give you the date in short format:
txtAccExp.Text = firstDate.ToShortDateString();
You can get the current short format from your culture with:
using System.Globalization;
:
var dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
Console.WriteLine(dtfi.ShortDatePattern);
In terms of fixing it, you could probably use the same method you used to populate the text field in the first place, so as to ensure it's the desired format:
txtActExp.Text = firstDate.ToString("dd/MMM/yyyy");

Convert String to Date time C# Error String was not recognize

I am trying to convert string to date time. My textbox contains a date only. My code is:
if (!String.IsNullOrEmpty(this.txtEOIDate.Text))
{
DateTime dt;
dt = DateTime.ParseExact(txtEOIDate.ToString(), "yyyy-MM-dd HH:mm tt",null);
invProject.created = dt;
}
txtEOIDate.Text contains "8/22/2014"
"invproject" is class object have datetime field "created"
error is String was not recognized as a valid DateTime.
on line
dt = DateTime.ParseExact(txtEOIDate.ToString(), "yyyy-MM-dd HH:mm tt",null);
I am using framework 4.0
UPDATE ANSWER:
Solution which work for me is:
string tes = txtEOIDate.Text.ToString().Trim();
DateTime dt = new DateTime(Convert.ToInt16(tes.Split('/')[2]), Convert.ToInt16(tes.Split('/')[0]), Convert.ToInt16( tes.Split('/')[1]));
invProject.created = dt;
Change this:
dt = DateTime.ParseExact(txtEOIDate.ToString(), "yyyy-MM-dd HH:mm tt",null);
To this:
dt = DateTime.ParseExact(txtEOIDate.Text, "yyyy-MM-dd HH:mm tt",null);
If you are using the standard textbox then txtEOIDate.ToString() is equals to:
System.Web.UI.WebControls.TextBox
Not:
8/22/2014
Update
If you want to parse a date which is in the format M/dd/yyyy. Why not just put it in the ParseExact. Like this:
DateTime.ParseExact(txtEOIDate.Text,"M/dd/yyyy",CultureInfo.InvariantCulture);
Why do you need ParseExact?
You cannot use ParseExact since you are not submitting all the values (hours, minutes etc.) I would do like this:
var dt = DateTime.Parse(txtEOIDate.Text);
UPDATE
This works:
if (!String.IsNullOrEmpty(this.txtEOIDate.Text))
{
var dt = DateTime.Parse(this.txtEOIDate.Text);
invProject.created = dt;
}
Firstly, ensure you are actually using txtEOIDate.Text, not txtEOIDate.ToString(). Secondly, I personally would use TryParse like this:
DateTime MyNewDT;
DateTime.TryParseExact(txtEOIDate.Text, "MM/dd/yyyy", null, System.Globalization.DateTimeStyles.None, out MyNewDT);
The format you input is not what you want it to be in but what format the string is currently in. As such, you are creating a DateTime variable that you can use.
txtEOIDate.ToString() does not return the Text of txtEOIDate
you should to change it to txtEOIDate.Text ...
and
change FormatString from yyyy-MM-dd HH:mm tt to M/dd/yyyy

Current date without time

How can I get the current date but without the time? I am trying to convert the date from the "dd.mm.yyyy" format to "yyyy-MM-dd", because DateTime.Now returns the time too, I get an error (String was not recognized as a valid DateTime.) when I try to do the following.
string test = DateTime.ParseExact(DateTime.Now.ToString(), "dd.MM.yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
Use the Date property: Gets the date component of this instance.
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
variable date contain the date and the time part will be 00:00:00.
or
Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy"));
or
DateTime.ToShortDateString Method-
Console.WriteLine(DateTime.Now.ToShortDateString ());
String test = DateTime.Now.ToString("dd.MM.yyy");
Have you tried
DateTime.Now.Date
String test = DateTime.Now.ToShortDateString();
it should be as simple as
DateTime.Today
This should work:
string datetime = DateTime.Today.ToString();
You can get current UTC date without time.
string currentDate = DateTime.UtcNow.ToString("yyyy-MM-dd");
As mentioned in several answers already that have been already given, you can use ToShorDateString():
DateTime.Now.ToShortDateString();
However, you may be a bit blocked if you also want to use the culture as a parameter. In this case you can use the ToString() method with the "d" format:
DateTime.Now.ToString("d", CultureInfo.GetCultureInfo("en-US"))
Try this:
var mydtn = DateTime.Today;
var myDt = mydtn.Date;return myDt.ToString("d", CultureInfo.GetCultureInfo("en-US"));
If you need exact your example, you should add format to ToString()
string test = DateTime.ParseExact(DateTime.Now.ToString("dd.MM.yyyy"), "dd.MM.yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
But it's better to use straight formatting:
string test = DateTime.Now.ToString("yyyy-MM-dd")

C# Getting Just Date From Timestamp

If I have a timestamp in the form: yyyy-mm-dd hh:mm:ss:mmm
How can I just extract the date from the timestamp?
For instance, if a timestamp reads: "2010-05-18 08:36:52:236" what is the best way to just get 2010-05-18 from it.
What I'm trying to do is isolate the date portion of the timestamp, define a custom time for it to create a new time stamp. Is there a more efficient way to define the time of the timestamp without first taking out the date, and then adding a new time?
DateTime.Parse("2010-05-18 08:36:52:236").ToString("yyyy-MM-dd");
You should use the DateTime type:
DateTime original = DateTime.Parse(str);
DateTime modified = original.Date + new TimeSpan(13, 15, 00);
string str = modified.ToString("yyyy-MM-dd HH:mm:ss:fff");
Your format is non-standard, so you'll need to call ParseExact instead of Parse:
DateTime original = DateTime.ParseExact(str, "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture);
You could use substring:
"2010-05-18 08:36:52:236".Substring(0, 10);
Or use ParseExact:
DateTime.ParseExact("2010-05-18 08:36:52:236",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd");
DateTime date;
if (DateTime.TryParse(dateString, out date))
{
date = date.Date; // Get's the date-only component.
// Do something cool.
}
else
{
// Flip out because you didn't get a real date.
}
Get the .Date member on the DateTime
DateTime date = DateTime.Now;
DateTime midnightDate = date.Date;
use it like this:
var x = DateTime.Now.Date; //will give you midnight today
x.AddDays(1).AddTicks(-1); //use these method calls to modify the date to whats needed.
The best (and fastest) way to do this is to convert the date to an integer as the time part is stored in the decimal part.
Try this:
select convert(datetime,convert(int, #yourdate))
So you convert it to an integer and then back to a data and voila, time part is gone.
Of course subtracting this result from the original value will give you the time part only.

Categories

Resources