web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

i need javascript code to capture currentdate and time into custom date and time field

(0) ShareShare
ReportReport
Posted on by 342

 i need javascript code to capture currentdate and time into custom date and time field , here i am using onsave event i am passing the currentdate and current time to custom field. Below code is returning null value

function updateCurrentDate() {

var currentDate = new Date();
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
var currentDate = month + "/" + day + "/" + year;

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
if (hours <12)
var suffix = "AM";
if (hours >= 12)
var suffix = "PM";

var currentTime = hours+":"+minutes+":"+seconds+" "+suffix;

var currentDateTime = currentDate +' '+currentTime ;


if (currentDateTime != null) {
Xrm.Page.getAttribute("date_A").setValue(currentDateTime );

}

}

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    If you are going to have to use a lot of date manipulation I'd recommend using http://momentjs.com/

    You can then turn it into a two line solution, I haven't used it in CRM but can't see why it wouldn't work. (once you have deployed the js to the form).

  • Suggested answer
    Community Member Profile Picture
    on at

    This will automatically set AM and PM. No need to create a string with date time format.Please refer this:

    community.dynamics.com/.../crm-2011-javascript-to-set-the-current-date-and-time

  • Suggested answer
    AbhiCode Profile Picture
    492 on at

    Hi Sandy,

    As you append your fileds so you need to add toString() function.

    var currentDate = new Date(); var date = currentDate.getDate().toString(); var month= (currentDate.getMonth() +1).toString(); var year = currentDate.getFullYear().toString(); var h = currentDate.getHours().toString(); var m = currentDate.getMinutes().toString(); var s = currentDate.getSeconds().toString();

    and as you just want to set current date and time in you datetime filed you can use a this one line code
    var currentDate = new Date();
    Xrm.Page.getAttribute("new_birthday").setValue(currentDate);

    Thanks
    Abhishek Bhansali
    Read My blog





  • Suggested answer
    Sandy Hello Profile Picture
    342 on at

    thanks Akhtar

  • Suggested answer
    Sandy Hello Profile Picture
    342 on at

    thanks for reply this worke well

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans