Thursday, April 29, 2021

How to format Date and Time with AM / PM

 I have used below code for formatting date time in a web-resource in dynamic crm.

// Formatting Date and Time with AM / PM 

function formatAMPM()

{

var date = new Date();

var hours = date.getHours();

var minutes = date.getMinutes();

var ampm = hours >= 12 ? 'PM' : 'AM';

hours = hours % 12;

hours = hours ? hours : 12; // the hour '0' should be '12'

minutes = minutes < 10 ? '0' + minutes : minutes;

var strTime = (date.getMonth()+1)+"/"+date.getDate()+"/"+ date.getFullYear()+" "+hours+':'+minutes +' '+ampm;

return strTime;

}


How to format Date in MMDDYYYY


// Formatting Date
this.setDateOptions = function () {
    Date.prototype.formatMMDDYYYY = function () {
        return this.getMonth() + "/" + this.getDate() + "/" + this.getFullYear();
    }

}



More Reference

How to validate end date to be greater than start date

Guidelines to write good JavaScript code in Microsoft Dynamic CRM

Refreshing Rollup field using JavaScript

Most common JavaScript methods for Dynamic 365 CRM


1 comment:

Ma'en.M said...

Hello ,

I have requirement same as this but from AM\PM to 24h format on CRM 2016 how can i accomplish this?

Any help would be appreciated.
Maen