Set moment date after setting the utcOffset - momentjs

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>

Related

SSRS Report Builder: Default Value of Date Parameter Unresponsive

I have a report that has Period, From Date, and To Date parameters. The Period is the number of months that should be between the From Date and To Date parameters by default. The From Date defaults to the system date.
The functionality I need is that if someone should change the From Date or Period parameters, the To Date should update automatically to the correct value. I also need to allow all three parameters to be manually alterable by the end user.
The default for the From Date works fine.
So far I have tried the following:
Using an SSRS expression to set the To Date Default value. This works correctly when I put a value into the Period parameter, however when I change that value the To Date does not update.
Using a dataset to set the To Date Default value. This has the same result as (1).
Using a dataset to set the Default and Available values to the calculated value. This updates correctly, however it becomes a none interactive parameter.
Using a helper parameter, with Default and Available values set as (3), then using this parameter as the default for To Date. This works correctly, producing the functionality I want, but it leaves a none interactive parameter on the parameters list, which is aesthetically undesirable.
Doing as (4), but making the helper parameter hidden. This reverts to the same functionality as (1).
Doing as (4), but making the parameter internal. This reverts to the same functionality as (1).
All parameters are set to Always Refresh. I don't understand why hiding the parameter makes it work differently, and I've run out of ideas for how to do this. Any suggestions would be very much appreciated.

Compare two dates using Date component only disregarding time

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.

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.

Change a date to the zone of another date

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());

How to set the entire date/time for an instance?

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.

Resources