I would like to set principal.AccountExpirationDate with a dateTimePicker1
if i do this - its work:
principal.AccountExpirationDate = new DateTime(2015, 12, 12);
but when i try to use the dateTimePicker1 its not work - this is my code:
string enddate = dateTimePicker1.Value.ToString("yyyy, MM, dd");
int val = Convert.ToInt32(enddate);
principal.AccountExpirationDate = new DateTime(val);
Im getting error.
how can i covert the dateTimePicker1 to a value that i can set in the principal.AccountExpirationDate?
You cannot convert DateTime or DateTime formated string to integer but you can take dateTime.Day, dateTime.Month or dateTime.Year as an integer separately.
int val = dateTimePicker1.Value.Day;
int month = dateTimePicker1.Value.Month;
int year = dateTimePicker1.Value.Year;
principal.AccountExpirationDate = new DateTime(year, month, val);
But principal.AccountExpirationDate = dateTimePicker1.Value is better one because dateTimePicker1.Value returns DateTime
Related
I need help in C# getting month names from current month, meaning user inputs a month(name) and will return the list of months from the starting month until the current month.
Example; user inputs "August" and current month is "December" so it should return "August, September, October, November, December".
I've done a few steps but still can't get to it.
1st try:
string pattern = ("MMMM/yyyy");
Console.WriteLine("Enter Month: MMMM/yyyy");
DateTime inpMonth = DateTime.ParseExact(Console.ReadLine(),pattern,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
string last = inpMonth.ToString("MMMM/yyyy");
DateTime date = DateTime.Now;
string curr = date.ToString("MMMM/yyyy");
2nd Try(new step):
//First Date
DateTime 1Date = new DateTime(2020, 12, 01);
//Second Date
DateTime 2Date =new DateTime(2019, 01, 01);
int month1 = (2Date.Month - 1Date.Month);
int month2 = (2Date.Year - 1Date.Year) * 12;
int months = month1 + month2;
string mon = months.DateTime.ToString("MMMM"); //trying to convert the month number to month name
Both try seems to not get any close result..
The following code will output to the result you requested:
var start = "August";
var today = DateTime.Today;
var date = new DateTime(today.Year, DateTime.ParseExact(start, "MMMM", CultureInfo.CurrentCulture).Month, 1);
while (date < today)
{
Console.WriteLine($"{date:MMMM}");
date = date.AddMonths(1);
}
I want to calculate year and month in my web application.
In my code, I have year 201604. I want to subtract the month by 3, So it should be 201601. I got this part work but now if I want to subtract another 3 monyths from 201601 again, the result should be 201511. I am not sure how to do this part. Help will be appreciated
Code
decimal str = "201604";
decimal year= (decimal) Calculations.ParseStringToDecimal(str.SubString(0,4), 0);
decimal month = (decimal) Calculations.ParseStringToDecimal(str.SubString(4,2), 0);
newMonth = month - 3;
string newDate = year + month
//Result: 201601.
//I want to subtract 201601 again, then the result should be 201511.
When working with dates use DateTime class which has been specially designed for this purpose:
String str = "201604";
DateTime date = DateTime.ParseExact(str, "yyyyMM", CultureInfo.InvariantCulture)
.AddMonths(-3);
// 201601
String newDate = date.ToString("yyyyMM");
DateTime threemonthprevDate = DateTime.ParseExact(str,"yyyyMM",CultureInfo.InvariantCulture).AddMonths(-3);
DateTime sixmonthprevDate = threemonthprevDate.AddMonths(-3));
Use ParseExact method instead.
string str = "201604";
DateTime dateTime = new DateTime(int.Parse(str.Substring(0, 4)), int.Parse(str.Substring(4, 2)), 1);
dateTime = dateTime.AddMonths(-3);
string resultString = dateTime.ToString("yyyyMM");
string str = "201604";
decimal year= (decimal) Calculations.ParseStringToDecimal(str.SubString(0,4), 0);
decimal month = (decimal) Calculations.ParseStringToDecimal(str.SubString(4,2), 0);
DateTime dateObj = new Date(year, month, 1);
dateObj.AddMonths(-3);
//201601
string newDate1 = dateObj.ToString("yyyyMM");
dateObj.AddMonths(-3);
//201511
string newDate1 = dateObj.ToString("yyyyMM");
how can I calculate if a date (in DateTime format) is 6 month later or not from my BirthDate (in DateTime format)?
Use DateTime AddMonth method
https://msdn.microsoft.com/ru-ru/library/system.datetime.addmonths(v=vs.110).aspx
var dat = new DateTime(2015, 12, 31);
var dat2 = new DateTime(2015, 12, 31);
if (dat.AddMonths(6) < dat2) { ... }
You should use DateTime.AddMonths :
DateTime dt;
DateTime birthDate;
if (dt <= birthDate.AddMonths(6))
{
}
enter your birth date, calculate your next birthday and compare the dates,
var born = new DateTime(1900, 02, 01);
var checkdate = DateTime.Now;
var nextBirthday = new DateTime(DateTime.Now.Year, born.Month, born.Day);
if (nextBirthday < DateTime.Now)
{
nextBirthday = new DateTime(DateTime.Now.Year + 1, born.Month, born.Day);
}
if (checkdate.AddMonths(6) < nextBirthday)
{
Console.WriteLine("date is 6 months later then birthday");
}
else
{
Console.WriteLine("wait for it");
}
DateTime birthDate=new DateTime(year,month,day);
DateTime dateToCompare = new DateTime(year, month, day);
if(dateToCompare >= birthdate.AddMonths(6))
{
//DoSomething
}
You could calculte the difference between dates using Subtract method and calculate how many months you have between these dates, for sample:
DateTime birthDay = /* some date */;
DateTime someDate = /* some date */;
var months = someDate.Subtract(birthDay).Days / (365.25 / 12);
This answer provides a good helper for Dates:
https://stackoverflow.com/a/33287670/316799
Assuming I can not change service that returns data, I am left with
var date = "20140231";
var scope = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture);
Clearly "20140231" is lazy way of saying end of February. What is the cleanest way to get last date of February with input of "20140231"?
There is 1 constraint - this should work with .net 2.0.
string date = "20140231";
DateTime result;
int year = Convert.ToInt32(date.Substring(0, 4));
int month = Convert.ToInt32(date.Substring(4, 2));
int day = Convert.ToInt32(date.Substring(6, 2));
result = new DateTime(year, month, Math.Min(DateTime.DaysInMonth(year, month), day));
February can have only 28 or 29 days depends on current year is a leap year or not.
It can't have 30 or 31 days in any year. That's why you can't parse your 20140231 string successfully.
You can clearly get the last day of February like;
DateTime lastDayOfFebruary = (new DateTime(2014, 2, 1)).AddMonths(1).AddDays(-1);
If your service always get year as a first 4 character, you can use .Substring() to get year and pass DateTime constructor as a year.
var date = "20140231";
string year = date.Substring(0, 4);
DateTime lastDayOfFebruary = (new DateTime(int.Parse(year), 2, 1)).AddMonths(1).AddDays(-1);
You could create a while, cut the date in pieces, and keep subtracting one from the day part until it is a valid date. This should really be fixed on the entry side though.
Try this:
var date = "20140231";
DateTime scope;
bool dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
while (!dateValid)
{
string yearMonth = date.Substring(0, 4);
int day = Convert.ToInt32(date.Substring(6, 2));
if (day > 1)
{
day--;
}
else
{
break;
}
date = yearMonth + day.ToString().PadLeft(2, '0');
dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
}
How can I get date from day of year in C#?
I have this code :
int a = 53; // This is the day of year value, that I got previously
string b = Convert.ToDateTime(a).ToString(); // Trying to get the date
I need to get the value 22.2.2014. But this doesn't work, what should I do? Thanks in advance.
int dayOfYear = 53;
int year = DateTime.Now.Year; //Or any year you want
DateTime theDate = new DateTime(year, 1, 1).AddDays(dayOfYear - 1);
string b = theDate.ToString("d.M.yyyy"); // The date in requested format
Assuming you want the current year?
int a = 53;
DateTime date = new DateTime(DateTime.Now.Year, 1,1).AddDays(a -1);
int a = 53;
var dt = new DateTime(DateTime.Now.Year, 1, 1).AddDays(a - 1);
string b = dt.ToString();
Use
DateTime.AddDays()
Initialize a date to start of the year , then just add a to that using this function
http://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx