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)

Getting only Date from Date time field using javascript

(0) ShareShare
ReportReport
Posted on by

Hi i have field on CRM appointment form which is in Date time format, i want to get only  date from that date time field, i tried few diffrent ways by using javascript either i am getting date in string format or date including time and jone(UTC). Any help please

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    Hi,

    you can "extract" the date components directly from the value.

    You can use this code for example:

    var dateFieldValue= Xrm.Page.getAttribute('datefieldname').getValue();
    // create the yyyy-mm-dd string
    var year = dateFieldValue.getFullYear()+"";
    var month = (dateFieldValue.getMonth()+1)+"";
    var day = dateFieldValue.getDate()+"";
    var dateFormat = year + "-" + month + "-" + day;

  • NKC Profile Picture
    on at

    Thanks for  your response but i want the out put as date( dd/mm/yyyy) format not in string. Thanks Any idea

  • Guido Preite Profile Picture
    54,086 Moderator on at

    But after where you need to put the value as dd/mm/yyyy?

  • NKC Profile Picture
    on at

    I need to set that value to another date field, which i am using in a workflow. Thanks

  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    With javascript you just need to create a new date a set to the field:

    var dateFieldValue= Xrm.Page.getAttribute('datefieldname').getValue();
    var year = dateFieldValue.getFullYear();
    var month = dateFieldValue.getMonth();
    var day = dateFieldValue.getDate();
    var dateOnly = new Date(year,month,day);
    Xrm.Page.getAttribute("new_datefield").setValue(dateOnly); 

  • NKC Profile Picture
    on at

    Thanks indeed

  • Community Member Profile Picture
    on at

    how to set time also?

    And how to set (createdon date only and 9PM)? Is it possible?

  • Community Member Profile Picture
    on at

    Why add + 1 for month?

  • Guido Preite Profile Picture
    54,086 Moderator on at

    if you need to show the date as a string you need to add 1 to the month because in JavaScript the months starts from 0, if you need the date as datetime object you don't need to add

  • PZ123 Profile Picture
    on at

    I am trying this script to get the value of the date only from a date and time field in CRM. I used this but it doesn't seem to work. When I use the Developer Tools the script breaks on startDate.getFullYear() + "";

    Any Ideas what I am doing wrong?

    var startDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();
    var year = startDate.getFullYear() + "";
    var month = (startDate.getMonth() + 1) + "";
    var day = startDate.getDate() + "";
    var dateFormat = year + "-" + month + "-" + day;

    Xrm.Page.getAttribute("new_dateauthorized").setValue(dateFormat);

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