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
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");
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
I have here 2 datepicker for start date and end date.
how can I get the first day and last day of the current month
rdpStartDate.SelectedDate = DateTime.Now;
rdpEndDate.SelectedDate = DateTime.Now;
DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
var now = DateTime.Now;
var first = new DateTime(now.Year, now.Month, 1);
var last = first.AddMonths(1).AddDays(-1);
You could also use DateTime.DaysInMonth method:
var last = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
var myDate = DateTime.Now;
var startOfMonth = new DateTime(myDate.Year, myDate.Month, 1);
var endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);
That should give you what you need.
Try this code it is already built in c#
int lastDay = DateTime.DaysInMonth (2014, 2);
and the first day is always 1.
Good Luck!
An alternative way is to use DateTime.DaysInMonth to get the number of days in the current month as suggested by #Jade
Since we know the first day of the month will always 1 we can use it as default for the first day with the current Month & year as current.year,current.Month,1.
var now = DateTime.Now; // get the current DateTime
//Get the number of days in the current month
int daysInMonth = DateTime.DaysInMonth (now.Year, now.Month);
//First day of the month is always 1
var firstDay = new DateTime(now.Year,now.Month,1);
//Last day will be similar to the number of days calculated above
var lastDay = new DateTime(now.Year,now.Month,daysInMonth);
//So
rdpStartDate.SelectedDate = firstDay;
rdpEndDate.SelectedDate = lastDay;
string firstdayofyear = new DateTime(DateTime.Now.Year, 1, 1).ToString("MM-dd-yyyy");
string lastdayofyear = new DateTime(DateTime.Now.Year, 12, 31).ToString("MM-dd-yyyy");
string firstdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("MM-dd-yyyy");
string lastdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1).ToString("MM-dd-yyyy");
I would like to get the "number of the day. For example:
January the first would be 1.
Febuary the first would be the 32.
So that we get up a whole year with 365 days.
Sry for my bad english :)
DateTime.DayOfYear is what you want
DateTime.Now.DayOfYear
or whatever yourDate is;
yourDate.DayOfYear;
Use DayOfYear property of any DateTime object:
var date = new DateTime(2012, 1, 1);
var dayNumber = date.DayOfYear;
There's a DateTime property named: DayOfYear
Console.WriteLine(DateTime.Now.DayOfYear);
Or for any date:
var d = new DateTime(2012, 8, 30);
Console.WriteLine(d.DayOfYear);
like this
private int GetDaysInAYear(int year)
{
int days = 0;
for (int i = 1; i <= 12; i++)
{
days += DateTime.DaysInMonth(year, i);
}
return days;
}
and do this (method above to get the number of days in year)
DateTime.Now.DayOfYear
DateTime.Now.DayOfYear.ToString()