date time condition in razor view - asp.net-mvc-4

How to set a condition to say if a data in a table is less than 5 days ago and then display a users information.
Below I can say less than date now.
#foreach (var item in Model)
{
if(item.RegisteredAt < DateTime.Now )
{
}
}

You can get the difference between two dates as a TimeSpan and use TotalDays property.
(DateTime.Now - item.RegisteredAt).TotalDays < 5

You can pass in a negative number to the AddDays function.
if(item.RegisteredAt < DateTime.Now.AddDays(-5)) {
If you wanted to ignore the time portion then you should compare them off the Date property.
if(item.RegisteredAt.Date < DateTime.Today.AddDays(-5)) {

if(item.RegisteredAt.Substract(DateTime.Now).Days.ToString().AsInt() < 5)
{
#* your expected code *#
}

Related

Entity Framework Core 6 - Date Comparision

I need to create a query to find records between a date range. I need to truncate the time for comparison. I came across a few threads suggesting to truncate time using DbFunctions.TruncateTime when comparing the dates.
I cannot find DbFunctions class in EntityFrameworkCore 6.0. How do I truncate time and compare the DateTime columns?
Edit: Excerpt from my query handler
IQueryable<TestReportItem> query = _context.TestReportItems;
DateTime requestDateStart = DateTimeOffset.FromUnixTimeMilliseconds(request.TestDateStart).LocalDateTime;
DateTime requestDateEnd = DateTimeOffset.FromUnixTimeMilliseconds(request.TestDateEnd).LocalDateTime;
if (request.FacilityId != 0)
{
query = query.Where(e => e.FacilityId == request.FacilityId);
}
if (request.TestDateStart != 0 && request.TestDateEnd != 0)
{
query.Where(e => e.TestDate.Date >= requestDateStart
&& e.TestDate.Date <= requestDateEnd);
}
var count = query.Count();
return count;
The request will be for a date range (dates converted into UnixTimeMilliseconds). I am using SqLite database.

How to test current date condition in behat?

I have feature that is based on current date, and the question is if it is good solution to write if condition in scenario. Simple example: if tested date is equal to current then other field is equal to 0 else equal 10. Meybe there are libraries to mock current date time in symfony 4.
You could write a step like:
Then I should see sysdate with format "Y-m-d" in the "example" field
Then for your context:
public function iShouldSeeSysdateWithFormatInTheField(string $format, string $key)
{
$field = $this->getSession()->getPage()->findField($key);
$value = (new DateTime())->format($format);
if (is_null($field->getValue())) {
assertEmpty($value);
}
if($value != $field->getValue()) {
throw new Exception('date does not match');
}
}

Search between Specific Times

I have a date field and 2 time fields(Start Time and End Time). I have to get Data from the date and entered in that date field and between Start and end times.
I am using Linq query.
result = result.Where(x => x.CreatedDate.ToLocalTime() > search.StartTime &&
x.CreatedDate.ToLocalTime() < search.EndTime);
I am using this but I am getting the following error.
LINQ to Entities does not recognize the method 'System.DateTime ToLocalTime()' method, and this method cannot be translated into a store expression
Please Help.
You can't use ToLocalTime() for Linq to Entities. Because it can't be translated to SQL. There is no equivalent to apply it.
So, why don't you convert your startTime and endTime by considering required time zone which is stored in the database. Opposite conversion should be like that;
//I use utc time to provide example.
//Convert your dates with required timezones which is stored in the database
var startTime = search.StartTime.ToUtcDateTime();
var endTime = search.EndTime.ToUtcDateTime();
result = result.Where(x => x.CreatedDate > startTime &&
x.CreatedDate < endTime);
As Panagiotis Kanavos pointed out there may be no equivalent argument. When you do have the correct type though you are essentially doing a conversion of one DateTime to another. Both the predicate values and the set should have their values preferably stored in the same TimeZone. Here is a simple console example to show what I mean.
public class TestData
{
public DateTime Dt { get; set; }
public TestData(DateTime dt)
{
Dt = dt;
}
public override string ToString() => Dt.ToString();
}
static void Main(string[] args)
{
var dt = new DateTime(2018, 1, 11);
var s = dt;
var e = dt.AddHours(3);
var dts = new List<TestData>();
for (int i = 0; i < 10; i++)
{
dts.Add(new TestData(dt.AddHours(i)));
}
Console.WriteLine("Dates as are");
dts.ForEach(Console.WriteLine);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Dates to local time are");
dts.ForEach(x => Console.WriteLine(x.Dt.ToLocalTime()));
Console.WriteLine(Environment.NewLine);
Console.WriteLine("searches are");
Console.WriteLine(s);
Console.WriteLine(e);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("searches to local time are");
Console.WriteLine(s.ToLocalTime());
Console.WriteLine(e.ToLocalTime());
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Weird results as one set is cast to local and other is not. Plus the cast is now performed on presentation");
dts.Where(x => x.Dt.ToLocalTime() >= s && x.Dt.ToLocalTime() <= e).ToList().ForEach(Console.WriteLine);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("expected results as both are uniform");
dts.Where(x => x.Dt >= s && x.Dt <= e).ToList().ForEach(Console.WriteLine);
Console.ReadLine();
}
Since different posters on SO are in different timezone the second result set will vary. But essentially I am in PST in the US so I see times for the second set eight hours prior to my first. You generally in .NET perform operations of storage to be UTC if your application is going to be worldwide or even country wide. Then you just deal with DateTimes and would only do a conversion on the client end for presentation to a potential end user. But most of the time not in the logic for predicates.

How to prevent user to enter specific times in datetimepicker?

I am using angular-bootstrap-datetimepicker. I know how to restrict the user from entering specific dates.
$dates.filter(function(date){
return (date.localDateValue() <= new Date().valueOf()-(24*60*60*1000));
}).forEach(function(date){
date.selectable = false;
})
This code insode startDateBefore render, prevents user from entering dates 24h past the current date.
But now I want users to enter spectic times only. Like user can select only times between 10:05am to 11:10pm. I am unable to do that. Documentation doesn't specify on how to add filter to hour and minutes. Any help on that.
In Angular Date Time Picker, the beforeRender function also takes parameter called view. So we can use the view==='hour' to restrict users from entering past hours. I was missing the code for hours.
function startDateBeforeRender ($view, $dates, $leftDate, $upDate, $rightDate) {
var today = moment().valueOf();
$dates.filter(function (date) {
return date.localDateValue() < today -(24*60*60*1000)
}).forEach(function (date) {
date.selectable = false;
});
// to restrict hours by admin
if ($view === "hour") {
// restict previous hours
$dates.filter(function (date) {
return date.localDateValue() < today
}).forEach(function (date) {
date.selectable = false;
})
}
}
date = new Date().valueOf();
if( date > 1000*(10*60+5) ){
if( date < 1000*(23*60+10) ){
//Code green, I repeat, CODE GREEN!
}
}

Difference between datepickers except sundays in infopath

How to get the no of days between two date picker controls in info path except sundays?
If It is possible let me know.
Thanks in advance.........
Using custom code you can do it easily. In this case I'm calculating the number of days (except Sundays) between a given date and today's date.
var navigator = this.MainDataSource.CreateNavigator();
string startDate = navigator.SelectSingleNode("/my:myFields/my:date_start", NamespaceManager).Value;
DateTime startDateTime = DateTime.ParseExact(startDate, "yyyy-MM-dd", null);
DateTime today = DateTime.Today;
int count = 0;
while (startDateTime > today)
{
today = today.AddDays(1);
if (today.DayOfWeek != DayOfWeek.Sunday)
{
count++;
}
}
I hope it helps