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?
Related
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.
I need to create a chrono::DateTime<Local> instance that is set to a specific date and time. For example, I need to create a DateTime<Local> instance that is set to something like 3/17/2019 at 4:43 PM (or 3/17/2019 at 16:43).
The documentation for the DateTime struct shows how to get the current date and time via the now function and plenty of support for getting time durations. There appears to be some confusing traits and conversion functions, but there doesn't appear to be anything that allows me to directly create a DateTime instance that represents a specific date and time.
Is it possible to create such an instance? If so, how?
There is a function called ymd for the TimeZone trait that returns a date. You can then call and_hms on this date to set a specific time.
use chrono::TimeZone;
let dt = chrono::Local.ymd(2019, 3, 17).and_hms(16, 43, 0);
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.
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.
I am facing an issue in my Flex application.
I am creating some array collections and storing date objects in it.In the later part of the application I will create 'advanceddatagridcolumns' with these array collections as the data sources.
Initially while creating the Array Col , I do have the 'formats' given by user, for each array collection eg. '1995/06/25' but in the later part I have no access to these formats. I want to display this dates in the data grid in same way as the user has specified. Right now, it displays it in the default format 'Sun Jun 25 00:00:00 GMT+0530 1995' instead of '1995/06/25'.
I have a common 'labelFunction' for these advancedatagridcolumns, and thus I can not use the DateFormatter as I 'formatstring' would be different for different columns.
So is there some way to display/retrieve the date in the same format as stored and not in the default way. Or while creating the date object can't I specify that I would always like it to be returned in some desired format.
The Date class in Flex as far, as my experiences go, is not very versatile. My hunch is that this is by no means possible with the regular framework capabilities.
However, if the date does not need to be changed after the user has input it, you might just want to save it as String and only create a temporary ArrayCollection of Dates for displaying them. This way you don't need to worry about the format changing because you're saving the original value.
How are you parsing the dates anyway? I mean that are you finding out the format yourself with RegExps or such, or using the parse method of the Date class?
just create a labelfunction specific to that column only and then you can use the formatstring