Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
There is an input string field "Januray 2020". I want to calculate total number of working days in the given month and year.
Can we do this in simple c#?
Since you've not defined what a work day is you can use this function to iterate over all the days in a month according to a predicate:
IEnumerable<DateTime> WorkDaysInMonth(int year, int month, Func<DateTime,bool> isNonWorkDay)
{
var now = new DateTime(year, month, 1);
while(now.Month == month)
{
if(!isNonWorkDay(now))
{
yield return now;
}
now = now.AddDays(1);
}
}
So, if you just want to create weekends as non work days then you'd get the total like this:
var weekdays = WorkDaysInMonth(2020, 1, d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday);
var total = weekdays.Count();
Likewise if you've got some calendar system that tells you about public holidays then you can add a call into the predicate to see if the supplied date is a holiday.
var s = "January 2020";
var d = DateTime.Parse(s);
Console.WriteLine(d);
var x = DateTime.DaysInMonth(d.Year, d.Month);
Console.WriteLine(x);
var w = Enumerable.Range(1, x)
.Select( i => new DateTime( d.Year, d.Month, i))
.Count( i => i.DayOfWeek != DayOfWeek.Saturday && i.DayOfWeek != DayOfWeek.Sunday );
Console.WriteLine(w);
Demo: https://dotnetfiddle.net/sp3qYo
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to check a string is combined by year and week number by c# code?
Like "202153",here "53" is the 53th week in 2021,and the week number cannot over 53 in 2021,
so,"202154" is not valid.
Thanks everyone for your suggestions, this is indeed an ambiguous question.I confirmed with my leader that our current system only needs to consider that the number of weeks does not exceed 53.
try this. you can change last line as per logic.
string date1 = "202154";
int year = Convert.ToInt32(date1.Substring(0, 4));
int weekNo = Convert.ToInt32(date1.Substring(4, 2));
//Validate week
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
DateTime date = new DateTime(year, 12, 31);//Get last date of year
Calendar cal = dfi.Calendar;
int weekOfYear = cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);//Get last week of the year.
bool answer = weekNo > weekOfYear?false:true;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do i go about creating a datetime based on only the following information:
Day of Week, Hour & Minuet.
I.e. I don't care what month it is or even what the date is (i don't have that info in the database).
I thought i could parse them as a string but is turning out to be more difficult than i thought.
Created on function for you it might be helpful to you ..
public DateTime CreateDayOfWeek(int DayOfWeek,int hour,int min)
{
DateTime dt = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,hour,min,0);
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysUntilTuesday = (DayOfWeek - (int)dt.DayOfWeek + 7) % 7;
// DateTime nextTuesday = today.AddDays(daysUntilTuesday);
dt = dt.AddDays(daysUntilTuesday);
return dt;
}
I have tested for several dates and its working for me ..
let me know if you have any issue ..
Here is .netFiddle
You can create your date like this...
var hour = 1; // you set this from code
var minute = 1; // set this from code
var now = DateTime.Now;
var tempDateTime = new DateTime(now.Year, now.Month, now.Day, hour, minute, 0);
// Make this enum whatever you want your date to be...
var num = (int)DayOfWeek.Sunday;
var dateForComparison = tempDateTime.AddDays(num - (int)tempDateTime.DayOfWeek);
Now dateForComparison holds a date that has your time values set and the day of week you have specified.
You said you don't care about what month or date it is, which makes me assume you want any date as long as it is the right day of week and time (hour and minute). You can do it like this:
var date = new System.DateTime(2016, 9, 25);
date = date.AddDays(dow).AddHours(hours).AddMinutes(minutes);
September 25, 2016 was a Sunday. Add the day of the week (Sunday = 0) and you get the correct day. Then add the hours and minutes. Of course, if you like you can pick any Sunday of any month/year to start.
You can create a function for build your date:
public DateTime BuildDate(Int32 day, Int32 hour, Int32 minute)
{
var now = DateTime.Now;
var initialDate = now.AddDays(((Int32)now.DayOfWeek + 1) * -1);
return new DateTime(initialDate.Year, initialDate.Month, initialDate.AddDays(day).Day, hour, minute, 0);
}
The day of week is start from sunday in this case.
You can use: DateTime.ToString Method (String)
DateTime.Now.ToString("ddd HH:mm") // for military time (24 hour clock)
More: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get report between 2 dates with PersianCalendar and I've already converted the dates. It does not show me the data about last day
This is my code:
public ActionResult FilterDate(DateFormViewModel model)
{
PersianCalendar pc = new PersianCalendar();
var dateFrom = pc.ToDateTime(model.FromYear, model.FromMonth, model.FromDay, 0, 0, 0, 0);
var dateTo = pc.ToDateTime(model.ToYear, model.ToMonth, model.ToDay, 0, 0, 0, 0);
var filter = db.Parts.Where(s => s.CreateDateTime >= dateFrom && s.CreateDateTime <= dateTo).ToList();
return View("Index", filter);
}
You're searching from midnight of the start date to 12 AM of the end date. You want to search from midnight of the start date to 11:59:59 PM of the end date.
You are missing data on the last day, right? (15 mordad)
The reason is that you are looking to the same day. having <= in the query does not help you.
for instance 1395/5/15 01:00:00 is greater than 1395/5/15 (your dateTo) and therefore it will not show up in the results
you have to check it with next day
var filter = db.Parts.Where(s => s.CreateDateTime >= dateFrom && s.CreateDateTime < dateTo.AddDay(1)).ToList();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
addition of two month and year in asp.net with c#.
if i select any one Month/year: like march/2014 and add(+) 12 month,
so it should be give the February/2014.
In this we can see Loan Period is: 12 (month) and below we can see loan start from month: 07(july/2014) so end of the load should be 06/2015. and the both month are in textbox it means they are string.
DateTime dt = new DateTime(2013, 1, 1);
dt.AddYear(1);
dt.AddMonths(2);
//Date is 2014, 3 (March), 1
Alternatively if you wish to substrat years and months you can use:
dt.AddYear(-1);
dt.AddMonths(-1);
//Date is 2013, 2 (February),1
Here you will not get 02/2015 if you add 12 months in 03/2014. You will get 03/2015 in result as shown below.
var inputString = "march/2014";
DateTime dt = DateTime.ParseExact(inputString, "MMMM/yyyy", CultureInfo.InvariantCulture);
var result = dt.AddMonths(12).ToString("MMMM/yyyy");
Will result in => "march/2015"
It seems that yous should add -1, not 12 months (if you want to get February from March):
String fromDate = "march/2013";
// result == "February/2013"
String result = DateTime
.ParseExact(fromDate, "MMMM/yyyy", CultureInfo.InvariantCulture)
.AddMonths(-1)
.ToString("MMMM/yyyy", CultureInfo.InvariantCulture);
In case that you want to add a year and two months (and so your example is incorrect)
String fromDate = "march/2013";
// result == "May/2014"
String result = DateTime
.ParseExact(fromDate, "MMMM/yyyy", CultureInfo.InvariantCulture)
.AddYears(1)
.AddMonths(2)
.ToString("MMMM/yyyy", CultureInfo.InvariantCulture);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to implement a Interface that returns the next weekday date after the date passed in. I need to use this code:
DiaryDate nextWeekday(DiaryDate originalDate);
public DiaryDate(int dayOfMonth, int monthOfYear, int year)
{
DayOfMonth = dayOfMonth;
MonthOfYear = monthOfYear;
Year = year;
}
public int DayOfMonth
{
get;
set;
}
public int MonthOfYear
{
get;
set;
}
public int Year
{
get;
set;
}
Is there a formula for working out which date will be a week day?
I'll just copy from my comment:
Construct a DateTime object and add 1 day while you have Saturday or Sunday. Then take the current day of the week at that time. If it doesn't loop you have your current day, if it does loop it will get you a monday. Or really just check if today is saturday/sunday. If yes: take monday, else take today
var someDate = new DateTime(year, monthOfYear, dayOfMonth).AddDays(1);
if(someDate.DayOfWeek == DayOfWeek.Saturday || someDate.DayOfWeek == DayOfWeek.Sunday) {
return DayOfWeek.Monday;
} else {
return someDate.DayOfWeek;
}
First convert the object into a DateTime to get access to all of the tools available through them:
DateTime date = new DateTime(originalDate.Year, originalDate.Month, originalDate.Day);
Then we can just add a day and then keep adding days while the date is a weekend:
date = date.AddDays(1);
while (date.DayOfWeek == DayOfWeek.Saturday ||
date.DayOfWeek == DayOfWeek.Sunday)
{
date = date.AddDays(1);
}
Then all you need to do is convert it back into your own date object, assuming you can't change your code to use DateTime instead.