Compare two dates using Date component only disregarding time - datetime

In C#, typically you can use DateTime.Today to get today's date disregarding the time component (e.g. the time component would be at midnight basically).
How do I do that using Luxom library?
I understand you can write DateTime.local() to get a Date object with current Time included, but is there no simple way to effectively disregard the time component so that I can perform a comparison with another Date object?
I can't find any relevant information in the Luxon documentation, though I may have missed something.
In standard JS, this would do it:
new Date(new Date().setHours(0,0,0,0));
But that seems awkward?

You can startOf('day') to set midnight to given DateTime:
"Set" this DateTime to the beginning of a unit of time.
then you can use toJSDate() to get the equivalent Javascript Date or other method like toISO, toISODate(), toFormat() etc to get the string representation of a DateTime.
Example:
const DateTime = luxon.DateTime;
console.log( DateTime.local().startOf('day').toJSDate() );
<script src="https://cdn.jsdelivr.net/npm/luxon#1.25.0/build/global/luxon.js"></script>
If you need to compare Luxon objects have a look at Comparing DateTimes section of the manual.

DateTime.local().toISODate();
This gave me what I wanted.

Related

Set moment date after setting the utcOffset

I'm looking to set a date on the YYYYMMDD that should be referencing to a date on a specific utcOffset. But I'm not sure what would be the best and elegant way to set this date after setting the utcOffset.
The close I can get is the following but its not the actual result I want. I need a way to first set the offset and then set the YYYYMMDD based on this offset.
moment.utc(ymdDate, 'YYYYMMDD').utcOffset(timeOffset)
Example:
In case I had a date like 20190420 that must be used on a moment object that should be referring to a different timezone and I do the following the date would result in April 19th.
moment.utc(20190420, 'YYYYMMDD').utcOffset(-300).format()
Result:
2019-04-19T19:00:00-05:00
Expected Result:
2019-04-20T00:00:00-05:00
You can use utcOffset passing true as second parameter
The utcOffset function has an optional second parameter which accepts a boolean value indicating whether to keep the existing time of day.
Passing false (the default) will keep the same instant in Universal Time, but the local time will change.
Passing true will keep the same local time, but at the expense of choosing a different point in Universal Time.
Here a live sample:
console.log( moment.utc(20190420, 'YYYYMMDD').utcOffset(-300, true).format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

SugarCRM Get an effective TimeDate from a SugarBean field

I must be missing something obvious, but it seems that I'm unable to find a way to get the TimeDate object from the value of a SugarBean field.
Let's say I get a specific Lead with this kind of call:
$lead = BeanFactory::retrieveBean('Leads', "18bfc69e-8cd4-11e7-ad08-000c29b1a36e");
then any call to this:
$lead->date_entered
will return a string value: "2017-08-29 16:05" (note the absence of seconds).
So then, for example, if I try to use such value to create a SugarTimeDate:
$TimeDate = new TimeDate();
$SugarTimeDate = $TimeDate->fromDb($lead->date_entered);
it will return false, since the value provided to fromDb() is not in the proper format (the seconds are missing).
When looking at the SQL table with Toad, I can see that the information is effectively stored in the database as a DateTime, with the value 08/29/2017 16:05:56. But the SugarBean object provides it as a text with a format that is incomplete.
So how can you get the effective SugarTimeDate, TimeDate or DateTime from a Field in a given SugarBean, ideally as an object?
I searched, and all the example I found was about creating a new date object from Now to set to a field in a SugarBean, but none to set a datetime field from an existing datetime field.
Any hint would be highly appreciated.
By playing around, and with some help from Patrick McQueen, it appears there 2 ways to get the effective date value of a field.
First solution I found was to do a SugarQuery with a select on the needed fields, which then returns the full date information, so "2017-08-29 16:05:56". A bit overkill, but it does the job.
The other solution brought up by Patrick is to use the fetcher_row array from the bean object, which will return the full date information also. So:
$lead->fetched_row['date_entered']
will returns also "2017-08-29 16:05:56".
So in any case an effective date is required ("round-trip" with a get then a set, or some sync requirement), the fetched_row[] is the solution, and the "direct" call to the field $bean->field is to be definitely avoided.
I wasn't 100% clear what you were trying to accomplish (see my comments), but I'm guessing that you want the fromUser() function instead, i.e.
$SugarTimeDate = $TimeDate->fromUser($lead->date_entered);
The reason why, is that Sugar prepares the data for the GUI (including formatting the date as per user preferences) at the point your code is being called. This includes stripping out the seconds. Doing the above fromUser() function will return a SugarDateTime object based on the current user's configured date format with a full date string as a "date" property. This, in turn, could be dealt with elsewhere by using this standard format.

How to calculate time difference between 2 DateTime fields in SSRS

I'm making a report that involves Tours lets say. I have one column populated by the tour_start_time datafield and another populated by tour_end_time. Then i have a column called Total Time. The Total Time column needs to tell me in hh:mm format what the difference between tour_start_time and tour_end_time is. So if tour_start_time returned 5:00 and tour_end_time returned 5:06 then Total Time should be 0:06. I can't seem to get this and the DateDiff stuff I've read all reference Parameters which isn't what I'm using. I need the data in a table. I tried this expression:
=DateDiff(minute,Fields!tour_start_time,Fields!tour_end_time)
But that didn't work and neither did any of the variants of that which I tried. I know im missing something as im new to this so sorry if it's a dumb question but i need someone that can explain it to me or at least give me another function to try.
Assuming your columns are actually DateTime data types, your DateDiff expression should look like this:
=DateDiff(DateInterval.Minute, Fields!tour_start_time.Value, Fields!tour_end_time.Value)
or
=DateDiff("n", Fields!tour_start_time.Value, Fields!tour_end_time.Value)
VB.NET DateDiff requires a DateInterval parameter; you can either specify the enumerator member (DateInterval.Minute) or its string representation (i.e. "n" for minute).
This will only return an integer value for the difference in minutes; it sounds like you need to then change this to a HH:mm string. Off the top of my head the easiest way to do this would be with some custom code in the report which takes this integer value as a parameter and construct the string based on this. Let me know if you need more information for this part.

Reporting Services: DateTime parameter not showing the time part when its midnight

I need to add a 'StartDate' parameter to a report in Reporting Services 2005, which should represent the current date, at midnight.
If I use Now() as expression, it will correctly use midnight in the calculations, but to the user, the time part will not be shown.
13/12/2011
which can be confusing because then the user doesn't really realise he or she can type in a time part.
How can I format this date to the following:
13/12/2011 00:00
while keeping the parameter as a DateTime datatype.?
Edit: I've checked the duplicate question and tried to apply it to my case, but run into the following phenomenom:
If I try:
=DateTime.Parse(Format(Now().Date().AddSeconds(1), "dd/MM/yyyy HH:mm:ss"))
Then the parameter shows "14/12/2011 00:00:01" which is confusing for the users, so I then tried the following:
=DateTime.Parse(Format(Now().Date().AddSeconds(1), "d/M/yyyy HH:mm"))
But then the parameter shows "14/12/2011" again! In other words, the time part is gone again!
Who invented this stuff? :P
Your formula with .Parse(Format(... causes redundant work that is messing things up. Try this:
=Now().Date().AddSeconds(.001)
Use Format. For example: Format(Parameters!SelectedDate.Value, "d/M/yyyy HH:mm")
Here is a list of DateTime identifiers for use with Format.
Edit:
I get it now, you want 00:00 to show up in the parameter input calendar in the report viewer. The time part won't show up when it's set to midnight. This is by design. If you're using 2005 or earlier, Jamie's solution won't work and you're stuck.

Comparing hours and minutes in AS3

Is there a way to compare the current time to a bunch of times (loaded from XML) and have it figure out which is the closest to the current time?
To expand on Boris's answer, you will indeed do this via the Date class.
You will want to convert each of your XML-read dates into a Date object (aka, a representation based on the number of milliseconds since Jan 1, 1970), probably via the parse() static method of the Date class:
// Taken from the linked webpage.
// Note there are many other formats that Date.parse supports, see the linked
// page for a list.
var dateParsed:String = "Sat Nov 30 1974";
var milliseconds:Number = Date.parse(dateParsed);
trace(milliseconds); // 155030400000
Once you have these date objects, you should create one more object for the current date/time, by calling the empty constructor Date(). Calling the valueOf() method on this new Date object will get you the number of milliseconds as above. Now you just have to loop through all of your XML dates and compare their value with the current date/time. The smallest difference is obviously the closest date/time.
you should be able to do this via the Date class in AS3.
( http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/index.html?Date.html& )
But I second Stephen's question : we can't help you any more without knowing your times format : are you comparing full text dates, timestamps, ...?
I think you can get most of questions answered here:
How can you save time by using the built in Date class?

Resources