Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by 340

 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

  • Suggested answer
    Sandy Hello Profile Picture
    Sandy Hello 340 on at
    RE: i need javascript code to capture currentdate and time into custom date and time field

    thanks for reply this worke well

  • Suggested answer
    Sandy Hello Profile Picture
    Sandy Hello 340 on at
    RE: i need javascript code to capture currentdate and time into custom date and time field

    thanks Akhtar

  • Suggested answer
    AbhiCode Profile Picture
    AbhiCode 492 on at
    RE: i need javascript code to capture currentdate and time into custom date and time field

    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
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: i need javascript code to capture currentdate and time into custom date and time field

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: i need javascript code to capture currentdate and time into custom date and time field

    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).

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans