var TimeNow = DateTime.Now.TimeOfDay;
var qq = from s in _EeHC_HrEntities.TransactionsSecretaries
where (EntityFunctions.CreateTime(s.TransactionTime.Hours,
s.TransactionTime.Minutes,
s.TransactionTime.Seconds) >
EntityFunctions.CreateTime(TimeNow.Hours,
TimeNow.Minutes,
TimeNow.Seconds))
I want compare between two times and display the data that data.time in dataebase > DateTimeNow.time but the result display not True
**becouse th 24 hours convert to 12 hours it will work thanx for answers**
var TimeNow = DateTime.Now.TimeOfDay;
var TimeHourNow = (TimeNow.Hours < 12) ? TimeNow.Hours : (TimeNow.Hours - 12);
var qq = from s in _EeHC_HrEntities.TransactionsSecretaries
where (EntityFunctions.CreateTime(s.TransactionTime.Hours, s.TransactionTime.Minutes, s.TransactionTime.Seconds) > EntityFunctions.CreateTime(TimeHourNow, TimeNow.Minutes, TimeNow.Seconds))
Related
I am trying to display the values of ApmaksasApmers (which is a Reference Value) which are closest to DateToCompare in comparison to GivenDate
For example:
DateToCompare =01.01.2022 and 01.01.2021
and GivenDate = 01.03.2022
I am trying to get the values which come from date 01.01.2022
Here is my code:
vm.ApmaksasApmērs.LookupSource = _nolasītMaksājumusQuery.Nolasīt()
.OrderBy(x => x.DateToCompare.Value > vm.GivenDate.Value ? vm.GivenDate.Value - x.DateToCompare.Value : x.DateToCompare.Value - vm.GivenDate.Value)
.Select(x => new KeyValuePair<Guid?, string>(x.Id, x.ApmaksasApmērs +" (" + x.PersonasLīdzmaksājumsProcentos + "%)".ToString())) ;
Here I am geting an error of Name:[ApmaksasApmērs],Type:[ReferenceValue],Message:[Don't currently support idents of type TimeSpan]
Is there a better way of doing this? Thanks in advance
Hi if DateToCompare is an array then we compare the absolute value of difference between each el with the given date and when the value is lowest that's the closest date
Here is how
//adding some data
DateTime[] DateToCompare = {
new DateTime(2001, 05,05),
new DateTime(2022,01,01),
new DateTime(2021,01,01)
};
DateTime GivenDate = new DateTime(2022, 05, 02);
//Get the absolute value of diference between first el and given date
TimeSpan span = abs(DateToCompare[0] - GivenDate);
DateTime closestDate = DateToCompare[0];
//new we check each el if the dif is lower
for (int i = 1; i < DateToCompare.Length; i++)
{
if (abs(GivenDate - DateToCompare[i]) < span)
{
span = abs(GivenDate - DateToCompare[i]);
closestDate = DateToCompare[i];
}
}
//get absolute value
TimeSpan abs(TimeSpan t)
{
return (t.TotalSeconds >= 0) ? t : -t;
}
Console.WriteLine(closestDate.ToShortDateString());
Here is what I did :
Create a list for all of the elements:
var saraksts = _nolasītMaksājumusQuery.Nolasīt().Where(x => x.DateToCompare <= vm.GivenDate).ToList();
Find the max value from the list:
var maxDate = list.Max(x=>x.DateToCompare)
Find all of the elements where MaxDate == DateToCompare:
vm.ApmaksasApmērs.LookupSource = list.Where(x => x.DateToCompare== maxDate).Select(x => new KeyValuePair<Guid?, string>(x.Id, x.ApmaksasApmērs +" (" + x.PersonasLīdzmaksājumsProcentos + "%)".ToString()));
Below is my code. I am only getting the difference between two dates, but I want the name of that month which comes between the from and to dates.
public static int GetMonthsBetween(DateTime from, DateTime to)
{
if (from > to) return GetMonthsBetween(to, from);
var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));
if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
{
return monthDiff - 1;
}
else
{
return monthDiff;
}
}
Based on your code you could substract the month difference from the "to" DateTime to get DateTime difference from your input.
public static List<DateTime> GetMonthsBetween(DateTime from, DateTime to)
{
if (from > to) return GetMonthsBetween(to, from);
var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));
if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
{
monthDiff -= 1;
}
List<DateTime> results = new List<DateTime>();
for (int i = monthDiff; i >= 1; i--)
{
results.Add(to.AddMonths(-i));
}
return results;
}
To get the name of the month just format the DateTime to "MMM".
var dts = GetMonthsBetween(DateTime.Today, DateTime.Today.AddMonths(5));
foreach (var dateTime in dts)
{
Console.WriteLine(dateTime.ToString("MMM"));
}
If you want the names of all months between two dates, use something like this:
var d1 = new DateTime(2015,6,1);
var d2 = new DateTime(2015,9,1);
var monthlist = new List<string>();
string format = d1.Year == d2.Year ? "MMMM" : "MMMM yyyy";
for (var d = d1; d <= d2; d = d.AddMonths(1))
{
monthlist.Add(d.ToString(format));
}
The full list is now in monthlist - you will want to return that from your method.
Assuming you're using Java and JodaTime there are several flaws in your code.
You cant use from > to to evaluate if a date is after an other. Use from.isAfter(to) instead.
JodaTime already supplies a method to calculate the amount of whole months between two given Dates Months.monthsBetween(start,end).
With the calculated month difference you can instantiate a new DateTime object that holds a date in your desired month and output its name via yourNewDateTimeObject.month().getAsText().
edit: Just found out you're using C# so ignore my text above this. Below here I will try to answer your question in C#.
Why dont you just subtract the from from the to date and obtain your difference?
The resulting TimeSpan can be used to determine the amount of whole months between your two given dates.
To obtain the resulting month name you could use yourDateTime.ToString("MMMM");
I have a 12 hour clock that calculates the difference between two times in decimal. It runs through the loop below but after testing I saw that when I type in a time between 12:00 and 12:59 (AM or PM) it posts the completely wrong time. The problem is that the 12th hour is a special case and doesn't need to have 12 added or subtracted from it. How can I fix this so it posts the correct time in decimal?
Also since I'm posting this I have another question; how can I easily calculate the total time in decimal?
Here's a picture of the GUI so you have an idea of what we're dealing with.
http://imgur.com/y7JezcC
protected void CalculateButton_Click(object sender, EventArgs e)
{
//Initializing
TextBox[] textboxesIn = new TextBox[7];
TextBox[] textboxesOut = new TextBox[7];
DropDownList[] dropdownIn = new DropDownList[7];
DropDownList[] dropdownOut = new DropDownList[7];
Label[] labels = new Label[7];
//Week 1 in textboxes
textboxesIn[0] = MondayW1InTextBox;
textboxesIn[1] = TuesdayW1InTextBox;
textboxesIn[2] = WednesdayW1InTextBox;
textboxesIn[3] = ThursdayW1InTextBox;
textboxesIn[4] = FridayW1InTextBox;
textboxesIn[5] = SaturdayW1InTextBox;
textboxesIn[6] = SundayW1InTextBox;
//Week 1 out textboxes
textboxesOut[0] = MondayW1OutTextBox;
textboxesOut[1] = TuesdayW1OutTextBox;
textboxesOut[2] = WednesdayW1OutTextBox;
textboxesOut[3] = ThursdayW1OutTextBox;
textboxesOut[4] = FridayW1OutTextBox;
textboxesOut[5] = SaturdayW1OutTextBox;
textboxesOut[6] = SundayW1OutTextBox;
//Week 1 in drop down list
dropdownIn[0] = MondayW1InDropDown;
dropdownIn[1] = TuesdayW1InDropDown;
dropdownIn[2] = WednesdayW1InDropDown;
dropdownIn[3] = ThursdayW1InDropDown;
dropdownIn[4] = FridayW1InDropDown;
dropdownIn[5] = SaturdayW1InDropDown;
dropdownIn[6] = SundayW1InDropDown;
//Week 1 out drop down list
dropdownOut[0] = MondayW1OutDropDown;
dropdownOut[1] = TuesdayW1OutDropDown;
dropdownOut[2] = WednesdayW1OutDropDown;
dropdownOut[3] = ThursdayW1OutDropDown;
dropdownOut[4] = FridayW1OutDropDown;
dropdownOut[5] = SaturdayW1OutDropDown;
dropdownOut[6] = SundayW1OutDropDown;
//Week 1 labels
labels[0] = MondayW1Label;
labels[1] = TuesdayW1Label;
labels[2] = WednesdayW1Label;
labels[3] = ThursdayW1Label;
labels[4] = FridayW1Label;
labels[5] = SaturdayW1Label;
labels[6] = SundayW1Label;
for (int i = 0; i < 7; i++)
{
DateTime dt = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
string timestring = dt.ToString("h:mm");
labels[i].Text = timestring;
DateTime timeIn = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
DateTime timeOut = DateTime.ParseExact(textboxesOut[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
if (dropdownIn[i].SelectedValue == "PM")
{
timeIn = timeIn.AddHours(12);
}
if (dropdownOut[i].SelectedValue == "PM")
{
timeOut = timeOut.AddHours(12);
}
labels[i].Text = (timeOut - timeIn).TotalHours.ToString("f2");
}
}
Try
DateTime timeIn = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0') + dropdownIn[i].SelectedValue.Text, "hhmm tt", CultureInfo.InvariantCulture);
You have to change HH to hh because you cant Parse 12AM to HH.
After that you can timeOut - timeIn without problem.
In every case, when you write 1 to 11, it would be AM by default and you can sum 12 hs, but when you write 12 always will be PM (AM would be 00), So:
1PM + 12 = 13
2PM + 12 = 14
..
11PM + 12 = 23
//Now your strange result:
12:45(PM) + 12 = 24:45 => 00:45 but one day after
3:00(PM) + 12 = 15
15hs (Day 0) - 00:45 (Day 1) is your negative result.
An easy way is resting 12hs when 12 is the case.
for (int i = 0; i < 7; i++)
{
DateTime dt = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
string timestring = dt.ToString("h:mm");
labels[i].Text = timestring;
DateTime timeIn = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
DateTime timeOut = DateTime.ParseExact(textboxesOut[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
if(timeIn.Hour == 12) timeIn = timeIn.AddHours(-12); //a easy way
if (dropdownIn[i].SelectedValue == "PM")
{
timeIn = timeIn.AddHours(12);
}
if (dropdownOut[i].SelectedValue == "PM")
{
timeOut = timeOut.AddHours(12);
}
labels[i].Text = (timeOut - timeIn).TotalHours.ToString("f2");
}
How do I covert JavaScript string "5:00 PM" to DateTime or TimeSpan when it get send to the MVC controller. I am using
bootstrap-timepicker
// usage
<script type="text/javascript">
$('#timepicker1').timepicker();
</script>
Javascript payload
{
Skip: 0
Status: []
Take: 15
DueTime: "1:00 PM" // keep in mind that this is a string
}
Server Object would be something like
class TimeSheet
{
public TimeSpan DueTime;
}
Use DateTime.Parse. Convert on server(on controller) when your string would transmit with your time.
http://msdn.microsoft.com/ru-ru/library/system.datetime.parse(v=vs.110).aspx
Okay so I read everyhting wrong hence the deleted answer.. !
But I'm not giving up ;)
Your bootstrap-timepicker, will give you a time as this "1:00 PM".
But before that we are going to look on the serverside to see what we can parse into a datetime object.
This is C# for parsing datetime.
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "15/08/2000 16:58"
format = "dd/MM/yyyy HH:mm"
result = DateTime.ParseExact(dateString, format, provider);
Now as you se your string wont look like that I'm going to assume that you want todays date!
This is function I tend to use most of the times when converting, to 24H clock.
function ConvertTimeformat(str) {
var time = str;
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if (AMPM == "PM" && hours < 12) hours = hours + 12;
if (AMPM == "AM" && hours == 12) hours = hours - 12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if (hours < 10) sHours = "0" + sHours;
if (minutes < 10) sMinutes = "0" + sMinutes;
//Creating the todays date in the right format
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!`
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
var todaysdate = dd+'/'+mm+'/'+yyyy +" " ; //<--I added an extra space!
var hoursNminutes = sHours + ":" + sMinutes
//CREATE THE RIGHT FORMAT FOR DATE.PARSEXACT "dd/MM/yyyy HH:mm"
var dateToParse = todaysdate + hoursNminutes
return dateToParse;
}
To Use the function do like this!
//Call it and provide your bootstrap time. And make it return to a variable
var dateToBeSentToServer = ConvertTimeformat("1:00 PM");
//OR With the bootstraptime as a variable
var dateToBeSentToServer = ConvertTimeformat(timevariable);
Now you can send dateToBeSentToServer to your serverside for parsing!
I know how to calculate the difference between two dates, but how do I calculate the time between a given date and the next 8 AM?
var now = DateTime.Now;
var tomorrow8am = now.AddDays(1).Date.AddHours(8);
double totalHours = ( tomorrow8am - now).TotalHours;
var now = DateTime.Now;
double diffHours = 24 - (now - now.Date).TotalHours + 8;
How about something like this
var now = DateTime.Now();
var target = DateTime.Today.AddDays(1).AddHours(8);
var result = (target - now).Hour;
Works well before 8am the same day.
DateTime now = DateTime.Now;
DateTime next8am =now.Date.AddHours(8);
if(now.TimeOfDay>TimeSpan.FromHours(8))
next8am.AddDays(1);
return next8am.Subtract(now).TotalHours