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);
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 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 using moment-timezone.
I have a date with a timezone and another date without one. I want to convert the latter date to the timezone of the former. This works:
let otherDateInZone = moment.tz(otherDate, dateInZone._z.name);
But I'm nervous about calling _z because I guess the underscore means it's not part of the supported API.
What's the "correct" way to do this?
Once an explicit time zone has been set, you can retrieve it with .tz()
let otherDateInZone = moment.tz(otherDate, dateInZone.tz());
The above will create a clone, leaving the original value of otherDate intact. If you don't care about that, then you can just call .tz(...) on the existing object to mutate it.
otherDate.tz(dateInZone.tz());
I'm adding an extension function to Moment and it needs to change the entire Moment instance to a new date/time value. However, the available Set methods only seem to allow setting specific units (ie. day, month, hour, second).
I understand that it would probably be possible to do:
this.set('year', year);
this.set('month', month);
...
but this seems ugly and possibly error-prone (if the values adjust for temporarily invalid date/times).
You can get Date from that moment values and then use setMinutes, setYear, setMonth etc. functions on that date object.
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?