Nitec-MichaelHenry
8/22/2019 - 11:41 AM

[UTC Dates] Storing Dates in UTC format in the database and pulling them out and displaying them in local time. C# #Dates #JavaScript

[UTC Dates] Storing Dates in UTC format in the database and pulling them out and displaying them in local time. C# #Dates #JavaScript

// ***** Get Data

tempJobWash = db.JobWash.Where(x => x.JobWashId == id).Select(x => new
                            {
RequiredDate = x.RequiredDate
   }).ToList().Select(j => new
                            {
                               RequiredDate = (j.ArrivedDate.HasValue ? j.ArrivedDate.Value.ToISOString(DateTimeKind.Utc) : null)
                           }).FirstOrDefault();
                           
                           
// ***** Display

<div data-contains="requiredDate" class="formSectionQuarterContainer  smalllivesearch input-group date" id="requiredDate">
        <fieldset>
            <div class="smallFormLabel">
                <span>Required By</span>
                <input value="{{{displayFormattedDate this.info.JobWash.RequiredDate this.dateFormat}}}" class="requiredDateDate" id="requiredDateDate" />
                <input value="{{{displayBPFormattedTime this.info.JobWash.RequiredDate }}}" class="requiredDateTime" id="requiredDateTime" />

            </div>
        </fieldset>
    </div>
    
    
// ***** Update 

  var requiredDateField = mainDiv.find('input.requiredDateDate').first();
        if (requiredDateField.data("DateTimePicker").date() == undefined)
        {
            nitec.notifications.warning("Please provide Required Date");
            return;
        };

        var requiredDate = requiredDateField.data("DateTimePicker").date();

data:{
                RequiredDate: requiredDate === null ? null : requiredDate.toISOString(),
}


c#
               var jobWashFound = ctx.JobWash.Where(x => x.JobWashId == JobWashDetails.JobWashId).FirstOrDefault();
                    if (jobWashFound != null)
                    {       
jobWashFound.WashDate = JobWashDetails.WashDate.Value.ToUniversalTime();
ctx.SaveChanges();
}