Insert birthdate to databse from day, month, year dropdown lists - c#

In my form I have three separate dropdown lists for birthday: Day, Month, and Year.
In my database, I have a column "birthday" with type date.
How will I convert those values from the dropdownlists in a specific date format to be accepted in the database?
Dropdown list values:
Day Month Year
1 Jan 1990
2 Feb 1991
3 Mar 1992
...and so on.
I tried this. It works but I know there's a better way:
DateTime bday = DateTime.Parse(String.Format("{0}/{1}/{2}", dropDay.SelectedValue, dropMonth.SelectedValue, dropYear.SelectedValue));

You should use DateTime and initilize it like this:
DateTime birthday = new DateTime(int year, int month, int day);

First you have to parse your Month string to int and than you should use DateTime and initialize it like this (as AitorFDK wrote) :
int month = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month
DateTime birthday = new DateTime(int year, int month, int day);

Related

C# date rolling 13 months values in global variable .NET

In C# how do I get the below datetime values in .NET. I am trying to look at todays date and trying to get a rolling 13 months. So last month minus 13 months which is december 2017 but need to get first day of the month at 00:00:00.000. Also trying to get an enddate as below which is end of last month.
Trying to get this and assign it to a variable in my code.
StartDate:
2017-12-01 00:00:00.000
EndDate:
2019-01-31 23:59:59.000
Whats the best way to get this?
Here you go
DateTime mydate = DateTime.Today;
//2017-12-01 00:00:00.000
DateTime StartDate = new DateTime(mydate.Year, mydate.Month, 1).AddMonths(-14);
//2019-01-31 23:59:59.000
DateTime EndDate = new DateTime(mydate.Year, mydate.Month, 1).AddSeconds(-1);
You can get the date with DateTime date = DateTime.Today.AddMonths(-14).AddDays(-(DateTime.Today.Day - 1));
.AddMonths(-13) yielded january 2018 for me

Converting Year, Month to DateTime

I am trying to create a DateTime object, but it seems to be giving me an error.
int month = "1"
int year = "2017"
DateTime date = new DateTime(year, month, DateTime.Day);
It doesn't seem to like DateTime.Day. It says an object reference is required for the non-static field.
How could I get today's day(16th) as a parameter? Also, I need the date to have hh:mm:sss... how could I do that?
Thanks for your help!
Use
var day = DateTime.Now.Day;
for today.
You can add hh:mm:sss to the date object in the constructor too:
DateTime date = new DateTime(year, month, DateTime.Now.Day, 10, 11, 12);
10 => hours
11 => minutes
12 => seconds
Of course you can use DateTime.Now.Hour etc. for the current values.
An ArgumentOutOfRangeException is thrown if the values are not valid for a real date, e.g. 30.2.xxxx.
You can print the date object in different formats, read the MS Documentation for all possibilities.
It should be:
int month = 1;
int year = 2017;
DateTime date = new DateTime(year, month, DateTime.Now.Day);
Take note, you declare integer without quotation marks:
int month = 1;
To convert it on 24 hour format with milliseconds as requested on comment:
string strResult = string.Format("{0:MM/dd/yyyy HH:mm:ss.fff}", date);
//Results: 02/17/2017 00:00:00.000
For 12 hour:
string strResult = string.Format("{0:MM/dd/yyyy hh:mm:ss.fff}", date);
//Results: 02/17/2017 12:00:00.000

c# adding correct month based on previous month

I am trying to add a month to a date and populate textboxes based on the previous month. For months that end on the 31st (and even February 28th) this works. But, if the previous month ended on the 30th and the next month ends on the 31st, it is either one day short or one day long. For example:
Previous start date: 4/1/2017
Previous end date: 4/30/2017
New start date: 5/1/2017
New end date: SHOULD BE 5/31/2017
Here is the code I have:
// Set the Start Date
DateTime dtNewStartDate;
dtNewStartDate = Convert.ToDateTime(txtRecentBeginDate.Text).AddMonths(1);
txtNewStartDate.Text = dtNewStartDate.ToString();
// Set the End Date
DateTime dtNewEndDate;
dtNewEndDate = Convert.ToDateTime(txtNewStartDate.Text).AddMonths(1);
txtNewEndDate.Text = dtNewEndDate.ToString();
This produces an end date of 6/1/2017 instead of 5/31/2017
EDIT: I was able to find what I was looking for from https://social.msdn.microsoft.com/Forums/en-US/218260ec-b610-4fa6-9d1b-f56f3438b721/how-to-get-the-last-day-of-a-particular-month?forum=Vsexpressvcs.
This solution accounts for leap years and getting the correct last day of the month for any circumstance. This is what I came up with:
// Set the End Date
int intYear = dtNewStartDate.Year;
int intMonth = dtNewStartDate.Month;
int intNumberOfDays = DateTime.DaysInMonth(intYear, intMonth);
DateTime dtNewEndDate = new DateTime(intYear, intMonth, intNumberOfDays);
txtNewEndDate.Text = dtNewEndDate.ToString();
You can just get the last day of the month for that month.
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year,
today.Month,
DateTime.DaysInMonth(today.Year,
today.Month));
This is what I typically do: Just take the year and month from the start date, create a new date based on that, add a month and subtract a day which ends up being the last day of the next month::
DateTime dtNewEndDate = new DateTime(dtNewStartDate.Year, dtNewStartDate.Month, 1)
.AddMonths(1)
.AddDays(-1);

C# How do I convert two or more integers into a date?

How do I convert two integers for instance 28 and 03 into a date like "28.03".
The integers should be input from the user and then converted to the date.
Also, how do I add days to the date?
Just an implementation for your example:
public static string GetDateString(int month, int day)
{
return new DateTime(DateTime.Now.Year, month, day).ToString("dd.MM");
}
To add days to a date you can use the DateTime.AddDays() method:
DateTime date = DateTime.Now;
DateTime otherDate = date.AddDays(7);
The links mentioned by #Giorgi and #D. Petrov are also very useful.
UPDATE:
Here is an example based on your comment.
class ConsoleApp
{
public void Main(string[] args)
{
int day = int.Parse(Console.ReadLine());
int month = int.Parse(Console.ReadLine());
string formattedDate = GetDateString(month, day);
Console.WriteLine(formattedDate);
// You cannot initialize a DateTime struct only with month and day.
// Because Year is not relevant we use the current year.
DateTime date = new DateTime(DateTime.Now.Year, month, day);
DateTime otherDate = date.AddDays(5);
Console.WriteLine(GetFormattedDate(otherDate));
}
public static string GetFormattedDate(DateTime date)
{
// The ToString() method accepts any custom date format string.
// Here is how you can create a custom date format string:
// https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
// dd: days in two digits
// MM: months in two digits
return date.ToString("dd.MM");
}
public static string GetDateString(int month, int day)
{
// Here we construct a DateTime struct
DateTime date = new DateTime(DateTime.Now.Year, month, day);
// Now we extract only the day and month parts.
return GetFormattedDate(date);
}
}
Well if 28 is day and 03 month - you can pass these parameters to the constructor of DateTime structure object. Once you initialize a DateTime object there are various ways to convert it as string. It also has AddDays method.
There is plenty of documentation about what you need (in particular - the DateTime structure). The most relevant information about your current need and the different ways to format yor string with the date you can find here: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
But as I mentioned before, there is plenty of information in the web.

Getting number of days for a specific month

how could i programmatically detect, how many days are there for a particular month & year.
It's already there:
DateTime.DaysInMonth(int year, int month);
should do it.
Something like this should do what you want:
static int GetDaysInMonth(int year, int month) {
DateTime dt1 = new DateTime(year, month, 1);
DateTime dt2 = dt1.AddMonths(1);
TimeSpan ts = dt2 - dt1;
return (int)ts.TotalDays;
}
You get the first day of the month, add one month and count the days in between.

Categories