Read custom format in datetimepicker c# - c#

I want to do dateTimePicker to choose and read only year. This is my code, but when on third row where is dateTimePicker1.Value it's someone else because when i open file is show me hour, date, month and year. I trying to change value with customformat but make me mistake and want to change to string. Whan I change to string like: string dt = dateTimePicker1.CustomFormat; started it give me the same result like value. Where is the problem?
dateTimePicker1.Format = DateTimePickerFormat.Custom; //1
dateTimePicker1.CustomFormat = "yyyy"; //2
DateTime dt = dateTimePicker1.Value; //3
fp.Seek(0, SeekOrigin.End); //4
bw.Write(dt.ToString()); //5

You need to use just ToString method:
dateTimePicker1.Value.ToString("yyyy");

Related

Parse a string to datetime format

I have here a little problem and would like to know where is my mistake and how to correct it.
string preConvDATE = monthCombobox2.Text + " " + datecombobox2.Text + ","+ "0000";
//lets say the comboboxes contain something like this "January 1";
DateTime DT = DateTime.ParseExact(preConvDATE, "MMMM d, yyyy 00:00:00", System.Globalization.CultureInfo.InvariantCulture);
string strDate = DT.ToString("yyyy-mm-dd");
try
{
//code that inserts strDate to a column in Mysql DB
}
catch
{
//msg
}
why "0000" for the year? because i was really planning to store just the month and date, but realized i could just store it as a datetime format with a year, and then at viewing the table i'd just use the Mysql MONTH() and DATE() function concatenated to view the Month and Date i stored from the monthCombobox2 and datecombobox2.
I also tried:
Convert.ToDateTime()
But still won't work.
How do i properly parse MMMM d,yyyy date format so that i could convert it to yyyy-mm-dd again and store it as datetime format in the database?
Thank you so much :)
As I can see, you are using text boxes to catch the value of month and date, instead of that you can use dropdowns which will display the month name but return the values like 01 for January and so on. It will be easy for you to format date formats. You don't want to save year that's okay, instead of setting 0000 you can set the current year. Now the problem with that you will not able to figure out the what exactly day was it (e.g Sunday, Monday and so on).
string preConvDATE = monthCombobox2.Text + "/" + datecombobox2.Text+"/" + DateTime.Now.Year.ToString();
DateTime DT = DateTime.ParseExact(preConvDATE, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
string strDate = DT.ToString("yyyy-MM-dd");
I hope this will solve your problem. Happy Coding!

c# Is it possible to subtract a day in the datetime format

I have the following simple example:
string dt = DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss");
I can't change the DateTime.Now, but I can change datetime format yyyy-MM-dd hh.mm.ss. Following this example the result must be today's date, but I need to get yesterday's date with the same parameters except day (year, month, hours, minutes and seconds). E.g. 2015-08-23 12.09.59 must be 2015-08-22 12.09.59. So is it possible to use some "-" operator or something else inside the datetime format to achieve the result?
If you want yesterday's date, you can do this
string dt = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd hh.mm.ss");
DateTime.AddDays() lets you add number of days, positive for future date, negative for past date.
E.g. 2015-08-23 12.09.59 must be 2015-08-22 12.09.59. So is it
possible to use some "-" operator or something else inside the
datetime format to achieve the result?
No, it's not possible inside the DateTime format. you can not change any thing. Because it is only for define format of the Date to display in string format. Any addition or subtraction can only be done before converting it to string format as suggested by "Arghya C".
Can you explain your limitation so we can solve your problem.
If you can only influence the date time pattern, than use the roundtrip format and parse the returning string back to a date time, add the calculation and format it into the desired format:
var dateTimeString = badLibrary.GetDateTime("o");
var dateTime = DateTime.Parse(dateTimeString, null, DateTimeStyles.RoundtripKind);
var newDateTime = dateTime.AddDays(-1);
return newDateTime.ToString("yyyy-MM-dd hh.mm.ss");

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

DateTime picker C# format

I have a DateTime picker to add arrival time to a list, I have 2 questions about it:
How can I get it to show dates like 12-Jan-2012 Instead of 12/01/12?
How can I get it to show the time after the date but not the current time, as thats what is shows atm.
My current code is not very advanced its just:
theVisit.ArrivalTime = DateTimePicker1.Value
Something like this will display the date and time:
DateTimePicker1.Value.ToString("d-MMM-yyyy hh:mm:ss");
To override the default DateTimePicker settings, you can do this:
DateTimePicker1.Format = DateTimePickerFormat.Custom;
DateTimePicker1.CustomFormat = "d-MMM-yyyy hh:mm:ss";
You can show a different time by modifying the format string, e.g.:
DateTimePicker1.CustomFormat = "d-MMM-yyyy 12:00:00";
or even
DateTime otherTime = DateTime.Now;
DateTimePicker1.CustomFormat = "d-MMM-yyyy " + otherTime.ToString("hh:mm:ss");
For it to show in that format in the picker, set the properties below
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd-MMM-yyyy hh:mm:ss";
You're looking for DateTime Format strings. There's a great article on them on MSDN.
There's two types:
Standard DateTime Formats
Custom DateTime Formats
You can use these to construct the datetime to look how you want it to.
For your example, you would use :
DateTimePicker1.Value.ToString("dd-MMM-yyyy hh:mm:ss")
First, to alter how your DateTimePicker displays DateTimes:
DateTimePicker1.CustomFormat = "d-MMM-yyyy hh:mm:ss";
DateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
Your code:
theVisit.ArrivalTime = DateTimePicker1.Value;
Would not change, because the DateTimePicker1.Value isn't any different, it's just displayed according to your format.
If you want the control to display a specific time (not the current time), you have to give the control that value:
DateTimePicker1.Value = new DateTime(2012, 12, 21, 23, 59, 59);
Otherwise it will display the current time as of the form creation.
You may check datetimepicker like this to check if it is empty.
if(DatTimePicker1.Text.Equals(" "))
{
MessageBox.Show("DateTimePicker is empty");
}

Split the date in c#

For Ex
You date enter in the various form in textbox
12/Augest/2010
augest/12/2010
2010/12/Augest
and out put is
three textbox First is day show= 12
textbox second is Months show= augest
textbox third is Year show= 2010
To parse/validate against three expected formats, you can use something like below. Given the pattern, once you know it is valid you could just use string.Split to get the first part; if you need something more elegant you could use TryParseExact for each pattern in turn and extract the desired portion (or re-format it).
string s1 = "12/August/2010",
s2 = "August/12/2010",
s3 = "2010/12/August";
string[] formats = { "dd/MMMM/yyyy", "MMMM/dd/yyyy", "yyyy/dd/MMMM" };
DateTime d1 = DateTime.ParseExact(s1, formats,
CultureInfo.CurrentCulture, DateTimeStyles.None),
d2 = DateTime.ParseExact(s2, formats,
CultureInfo.CurrentCulture, DateTimeStyles.None),
d3 = DateTime.ParseExact(s3, formats,
CultureInfo.CurrentCulture, DateTimeStyles.None);
Use DateTime.Parse(String, IFormatProvider) or DateTime.ParseExact to convert the string into DateTime.
Then you can extract the day, month and year using the corresponding properties.
Use DateTime.Parse(s). See MSDN
Then you can get the individual parts of a DateTime structure.
e.g.
DateTime date = DateTime.Parse("some input date string");
string day = DateTime.Day.ToString();
string month = DateTime.Month.ToString();
string year = DateTime.Year.ToString();
date dt date.Parse(txtBox.text);
txtBox1.Text = dt.Day.ToString();
txtBox2.Text = dt.ToString("MMM");
txtBox3.Text = dt.Year.ToString();
date.Parse might throw depending on the string you give it, but then you can fall back by trying to parse it using a different culture.
Edit: Added an M

Categories