Getting first and last day of the current month - c#

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");

Related

Return month names from current month

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);
}

Days between two dates in asp.net

I find the number of days between the date of the employee's employment and the date of the day, and multiply by the daily amount. The only complaint is that when I find out the number of days between two dates, it calculates over 31 days for the months that draw 31 days naturally. I need to trade over 30 days while I get the dates between two dates.
How can I do that?
Do you want something like this?
DateTime date1 = new DateTime(2016, 10, 3);
DateTime date2 = new DateTime(2016, 11, 3);
var numberOfDays = date2.Subtract(date1).TotalDays;
I Hope this is what you wanted:
DateTime firstDay = DateTime.ParseExact("2016-10-03", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
DateTime lastDate = DateTime.ParseExact("2016-11-03", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
double daysBetween = (lastDate - firstDay).TotalDays;
if you are just interested in full month, you can use following code
var normalisedDays = ((lastDate.Year - firstDate.Year) * 12 + lastDate.Month - firstDate.Month) * 30;
in Controll :
DateTime Date_1 = Date_Start;
DateTime Date_2 = Date_End;
TimeSpan difference = Date_2 - Date_1 ;
var days = difference.TotalDays;
in Script :
<script>
function calculateDifference()
{
var Date_Start= document.getElementById("Date_Start").value;
var Date_End= document.getElementById("Date_End").value;
var Date_StartSplit = Date_Start.split("/");
var Date_EndSplit = Date_End.split("/");
var StartDate = new Date(Date_StartSplit[2], Date_StartSplit[0] - 1, Date_StartSplit[1]);
var EndDate = new Date(Date_EndSplit[2], Date_EndSplit[0] - 1, Date_EndSplit[1]);
var res = Math.abs(StartDate - EndDate) / 1000;
var days = Math.floor(res / 86400);
document.getElementById("Nombre_days").value = days;
}
</script>

Check if any dates within a range fall within a specified month

I am at a bit of a loss as to the best approach. Let's say I have the following:
//Get the first and last day of the month -- ex February
int month = DateTime.ParseExact("February", "MMMM", new CultureInfo("en-US")).Month;
var now = DateTime.Now;
var firstOfMonth = new DateTime(now.Year, month, 1);
var lastOfMonth = new DateTime(now.Year, month, DateTime.DaysInMonth(now.Year, month));
var coverageStartDate = new DateTime(now.Year, 1, 1);
var coverageEndDate = new DateTime(now.Year, 2, 15);
I am trying to create a check to see if the date range of coverageStartDate and coverageEndDate falls in the month of February. Keep in mind that the values could also look like:
var coverageStartDate = new DateTime(now.Year, 2, 3);
var coverageEndDate = new DateTime(now.Year, 2, 10);
As long as there is a single date in the coverage start / end date range falls in the month of February, I would want to return true.
With DateTime you can use >=, <=, etc.
Adopting your code, I would do something like this:
var firstOfMonth = new DateTime(now.Year, month, 1, 0, 0, 0);
var lastOfMonth = new DateTime(now.Year, month, DateTime.DaysInMonth(now.Year, month), 23, 59, 59);
var coverageStartDate = new DateTime(now.Year, 1, 1);
var coverageEndDate = new DateTime(now.Year, 2, 15);
if(coverageStartDate <= lastOfMonth && coverageEndDate >= firstOfMonth)
{
// Do something
}
This code does not requires that years have to be the same, so the coverage time can span multiple years.

How to get date from day of year

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

How to get the first day and the last day of the current year in c#

How do I get the first day and the last day of the current year in c#
This?
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31);
Try this:
var firstDay = new DateTime(DateTime.Now.Year, 1, 1);
var lastDay = new DateTime(DateTime.Now.Year, 12, 31);
None of the answers here actually account for the last day.
In my opinion, the correct way to do this would be:
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year , 1, 1);
DateTime lastDay = firstDay.AddYears(1).AddTicks(-1)
Hope this would be valuable to someone :)
Why not getting the first day of the next calendar year (month 1, day 1) and subtract one day.

Categories

Resources