We have a button "Start" that populates the dateStarted field to DateTime.Now..
When retrieving datetime, it always shows the server's date/time instead of user's local timezone version. How do i make it work like lastModifiedDateTime/CreatedDateTime that whenever we view it it's formatted as user's local timezone. I tried UseTimeZone = true/false but nothing is working
Here is my code that is not working
#region DateStarted
public abstract class dateStarted : PX.Data.IBqlField
{
}
protected DateTime? _DateStarted;
[PXDBDateAndTime(DisplayNameDate = "Date Started", DisplayNameTime = "Time", UseTimeZone = true)]
public virtual DateTime? DateStarted
{
get
{
return this._DateStarted;
}
set
{
this._DateStarted = value;
}
}
#endregion
Look up the code of class
PXDBDateAndTime
inSource Code
screen SM204570. For debugging purpose you can copy the whole attribute and rename it to something else likePXDBDateAndTimeDebug
:With that approach you can debug
SetUseTimeZone
andGetTimeZone
methods. The time zone used is coming fromLocaleInfo.GetTimeZone
method and you should debug that too:The issue here is that from your question we can't tell if there's actually a problem with Acumatica time zone handling or if the user profile time zone is properly configured in your instance or if the result you're expecting is actually a valid ISO conversion as done by the DotNet framework. Debugging step by step should reveal what's going on.
After analyzing how CreatedDateTime and LastModifiedDatetime and other dates behave the same, the problem is in the input. So i created the following code to save the correct datetime with regards to the current user's timezone.
and the implementation:
It sounds like just using
PX.Common.PXTimeZoneInfo.Now
is enough to do the job.PX.Common.PXTimeZoneInfo
also hasUtcNow
,UtcToday
, andToday
if needed