some rows are missing on results of my query [closed] - c#

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

Related

How to validate a string is combined by year and week number in c#? [closed]

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;

Get number of days in any month without saturday and sunday? [closed]

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

c# datetime create Day of Week Hour and Min [closed]

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

Check Date Equals 1st Date of Month C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
User can input any date, month and year like 12/12/2013, 1/1/2014, 7/5/2014, 5/1/2012 in MM\DD\YYYY format.
How to check the date is first date of month ?
If the user entry is not first date of month, I want to modify that entry to 1st date of month. In my Examples, I want
12/12/2013 as 12/1/2013
1/1/2014 as 1/1/2014(No Change)
7/5/2014 as 7/1/2014
5/1/2012 as 5/1/2012(No Change)
Thanks
DateTime date = ... // your original date here...
// Don't bother checking, just create a new date for the 1st.
date = new DateTime(date.Year, date.Month, 1);
UPDATE:
The OP has apparently changed the specs:
DateTime date = ... // your original date here...
if (date.Day != 1)
date = new DateTime(date.Year, date.Month, 1).AddMonths(1);
(let the .AddMonths() method worry about the year rolling over in December...)
IMO, since you have a definite format you expect from users (MM\DD\YYYY) why not do a simple split and dig your hit:
string arbitDate = "4/3/2014";
string UsersFirstDay = arbitDate.Trim().Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();//index 1 is the DD part - according to your format
UsersFirstDay = (UsersFirstDay == "1") ? UsersFirstDay : "1";
Pass your date to this function:
public static void ConvertToFirstDate(ref DateTime dt){
TimeSpan ts = dt.Subtract(new DateTime(dt.Year, dt.Month, 1));
dt = dt.AddDays(-ts.Days);
}

addition of two month and year in asp.net with c# [closed]

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

Categories