I have a scenario, my database update table with one record every few minutes with a specific date. now I need to extract the month to check if month name equal to (April to October).
How to achieve that.
Please assist.
You can get the month name as below:
var dateTime = DateTime.Now;
int month = dateTime.Month;
if (month >= 4 && month <= 10)
{
// Month is between 4 & 10...
// Get the full month name: dateTime.ToString("MMMM")
}
Related
I was looking for a way to fetch the same day of the current week as a year ago. For example, today is:
August 10th 2022 - Wednesday.
Assume this is the check-in date, the check-out date I expect to get is:
August 11, 2021 - Wednesday.
Because it's the same day (Wednesday) as last year. But I need to take leap years into account, so I need to see if the current year is a leap year and if it is, if it has passed the 29th of February, the same with the date last year.
How to do this using .net core ? I thought of something like:
private DateTime GetDayOneYearBefore()
{
if(DateTime.IsLeapYear(DateTime.Today.Year) && DateTime.Today.Month > 2){
return DateTime.Today.AddDays(-365);
}
else if(DateTime.IsLeapYear(DateTime.Today.Year) && DateTime.Today.Month <= 2){
return DateTime.Today.AddDays(-364);
}
}
Since you mention the "same week" I suppose you want to get the same day of the week in the same week number?
If so, you can do the following:
// In the System.DayOfWeek enum Sunday = 0, while Monday = 1
// This converts DateTime.DayOfWeek to a range where Monday = 0 and Sunday = 6
static int DayOfWeek(DateTime dt)
{
const int weekStart = (int)System.DayOfWeek.Monday;
const int daysInAWeek = 7;
return (daysInAWeek - (weekStart - (int)dt.DayOfWeek)) % daysInAWeek;
}
var calendar = CultureInfo.CurrentCulture.Calendar;
var weekNum = calendar.GetWeekOfYear(DateTime.Today, CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Monday);
var todayLastYear = DateTime.Today.AddYears(-1);
var lastYearWeekNum = calendar.GetWeekOfYear(todayLastYear, CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Monday);
var sameWeekLastYear = todayLastYear.AddDays(7 * (weekNum - lastYearWeekNum));
var sameDaySameWeekLastYear = sameWeekLastYear.AddDays(DayOfWeek(DateTime.Today) - DayOfWeek(sameWeekLastYear));
As you might notice there's a little convertion method, since I normally work with Monday being the first day of the week. If you prefer a different day to be the first day of the week, simply replace System.DayOfWeek.Monday with which ever day you'd like.
See this fiddle for a test run.
I want to compare a given date to today and here is the condition: If provided date is greater than or equal to 6 months earlier from today, return true else return false
Code:
string strDate = tbDate.Text; //2015-03-29
if (DateTime.Now.AddMonths(-6) == DateTime.Parse(strDate)) //if given date is equal to exactly 6 months past from today (change == to > if date has to be less 6 months)
{
lblResult.Text = "true"; //this doesn't work with the entered date above.
}
else //otherwise give me the date which will be 6 months from a given date.
{
DateTime dt2 = Convert.ToDateTime(strDate);
lblResult.Text = "6 Months from given date is: " + dt2.AddMonths(6); //this works fine
}
If 6 months or greater than 6 months is what I would like for one
condition
If less than 6 months is another condition.
Your first problem is that you're using DateTime.Now instead of DateTime.Today - so subtracting 6 months will give you another DateTime with a particular time of day, which is very unlikely to be exactly the date/time you've parsed. For the rest of this post, I'm assuming that the value you parse is really a date, so you end up with a DateTime with a time-of-day of midnight. (Of course, in my very biased view, it would be better to use a library which supports "date" as a first class concept...)
The next problem is that you are assuming that subtracting 6 months from today and comparing it with a fixed date is equivalent to adding 6 months to the fixed date and comparing it with today. They're not the same operation - calendar arithmetic just doesn't work like that. You should work out which way you want it to work, and be consistent. For example:
DateTime start = DateTime.Parse(tbDate.Text);
DateTime end = start.AddMonths(6);
DateTime today = DateTime.Today;
if (end >= today)
{
// Today is 6 months or more from the start date
}
else
{
// ...
}
Or alternatively - and not equivalently:
DateTime target = DateTime.Parse(tbDate.Text);
DateTime today = DateTime.Today;
DateTime sixMonthsAgo = today.AddMonths(-6);
if (sixMonthsAgo >= target)
{
// Six months ago today was the target date or later
}
else
{
// ...
}
Note that you should only evaluate DateTime.Today (or DateTime.Now etc) once per set of calculations - otherwise you could find it changes between evaluations.
Try with this
DateTime s = Convert.ToDateTime(tbDate.Text);
s = s.Date;
if (DateTime.Today.AddMonths(-6) == s) //if given date is equal to exactly 6 months past from today (change == to > if date has to be less 6 months)
{
lblResult.Text = "true"; //this doesn't work with the entered date above.
}
replace == with >= or <= according to your needs
i am trying to get day of the week only if entered date is falling between last Monday to current date i.e if today is 30/10/14 the output should be Thursday else just show date entered
here is what i am trying
DateTime aa = Convert.ToDateTime(rr.detail);
if (DateTime.Now.Subtract(aa).Days < 7)
{
// but this is not i am looking for i want only if aa
// is falling between last monday and less than 7 days
}
so any idea how to achieve it?
If I understand correctly, the steps I'd take are:
1) Get the date of the previous Monday
2) Get the user's date
3) Check if the user's date is after the previous Monday (in which case
show the day)
//Get the date of the previous Monday
DateTime prevMonday = DateTime.Now;
while(prevMonday.DayOfWeek != DayOfWeek.Monday)
prevMonday = prevMonday.AddDays(-1);
//get user's date
DateTime aa = Convert.ToDateTime(rr.detail);
//check if the user's date is after the Monday
if (aa > prevMonday && aa <= DateTime.Now.Date)
Console.WriteLine(aa.DayOfWeek);
else
Console.WriteLine(aa);
I have Holiday EF Entity:
public class Holiday {
public int HolidayId {
get;
set;
}
public DateTime StartDate {
get;
set;
}
public DateTime EndDate {
get;
set;
}
public bool IsActive {
get;
set;
}
}
I need to get all records that has at least one day within the current month.
For example:
StartDate = "2014-06-29"
EndDate = "2014-07-03"
If current month is July then I should get that record, because after that I will have to create a List<int> of all the days affecting current month. So in the last sample I will have to create a List<int> of:
1,2 and 3 of July.
For example:
StartDate = "2014-07-23"
EndDate = "2014-08-01"
I should get that record also.
I was doing:
var holidays = Holidays.Where(h=> h.IsActive == true && h.StartDate < currentDate && h.EndDate > currentDate).ToList();
But, the query is not doing what I need.
Any clue?
What about this:
.Where(h => h.IsActive == true && (h.StartDate.Month == currentDate.Month || h.EndDate.Month == currentDate.Month)).ToList();
UPDATED:
This won't work when I have a date range where it's month is not equal to currentDate.Month. Like:
StartDate = "2014-06-29"
EndDate = "2014-08-03"
The solution that worked is:
Maybe is not the cleanest.
var holidays = Uow.HolidayRepository.GetAllReadOnly().Where(h => h.IsActive == true && (h.StartDate.Month == currentDate.Month || h.EndDate.Month == currentDate.Month) || (currentDate.Month > h.StartDate.Month && currentDate.Month < h.EndDate.Month)).ToList();
Neither test is sufficient: if the current date is today in then the first test will miss any date ranges that don't include the current date so if today is the first of the month any date range starting on the second although valid won't be caught. In addition single day ranges - unless you are including times won't be caught because the start and end dates could be equal to the current date but can never be greater than and less than - depends how you spec your date range.
The second test fails firstly because every year has months 1- 12 so not only will you be picking records for this year but for all years and secondly you miss any periods that start and end outside of the month.
Creating a list of days to compare doesn't sound efficient minimum number of runs 28 max 31.
So you need to test for ranges that either start in the current month or end in the current month as per Daniels code but in addition you need to test where the start date is before the current month and the end date is after the current month I think that should cover all cases?
I think this will do,please try it out:
int month = DateTime.Now.Month;
var result = Holidays.AsEnumerable().Where(h => Enumerable.Range(h.StartDate.Month, (h.EndDate.Month - h.StartDate.Month) + 1).Contains(month));
user just enter the day of week. For instance user enter friday. I need to find the exact date of given day and format will be like dd.MM.yyyy.
But I don't know how I do it.
Example:
label1 - Friday (entered by user)
label2 - 08.06.2012 (found by system)
label1 is just a string (just Friday). It's coming from webservice as a string variable. I need to find the date and compare with today, If it's not equal or small than today I give date of upcoming Friday, else I give the date of the Friday the week after.
"If it's not equal or small than today I give exact date, else I give next week date. "
Assuming that means that you return always the next date in future with the given day of week, the only exception is when today is the given day of week.
public static DateTime getNextWeekDaysDate(String englWeekDate)
{
var desired = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), englWeekDate);
var current = DateTime.Today.DayOfWeek;
int c = (int)current;
int d = (int)desired;
int n = (7 - c + d);
return DateTime.Today.AddDays((n >= 7) ? n % 7 : n);
}
Let's test:
DateTime Monday = getNextWeekDaysDate("Monday"); // 2012-06-11
DateTime Tuesday = getNextWeekDaysDate("Tuesday"); // 2012-06-05 <-- !!! today
DateTime Wednesday= getNextWeekDaysDate("Wednesday"); // 2012-06-06
DateTime Thursday = getNextWeekDaysDate("Thursday"); // 2012-06-07
DateTime Friday = getNextWeekDaysDate("Friday"); // 2012-06-08
Create enum of days (i.e. monday - 0, tuesday - 1, etc);
Get DateTime.Now DayOfWeek and cast in some way it to your enum value.
Calculate difference between Now.DayOfWeek and user's day of the week;
Use DateTime.AddDays(difference).DayofTheWeek;
get current time with DateTime.now
Current day is DateTime.Now.DayOfWeek
Then get the day of week your user entered
Then your result is DateTime.now.AddDays( NowDayOfWeek - UserDayOfWeek).