Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting only Date from Date time field using javascript

Posted on by 4,440

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

  • Ravi Fulani Profile Picture
    Ravi Fulani 814 on at
    RE: Getting only Date from Date time field using javascript

    Hi Mohit, were you able to solve this part

  • Mohit Patel Profile Picture
    Mohit Patel 25 on at
    RE: Getting only Date from Date time field using javascript

    Hello,

    I need to read Date value of one DateTime field in my Dynamics CRM Form using JavaScript.

    I am able to read the value but it is not exactly same what I can see in CRM.

    In CRM Form, selected value is: 17th May 2018

    JavaScript Code return value as: Wed May 16 2018 00:00:00 GMT+0530 (India Standard Time)

    CRM logged in user time-zone: GMT +10:00 Sydney

    My time-zone: +5:30 India

    JavaScript Code I am using is:

    var dateFieldValue= Xrm.Page.getAttribute('MyDateField').getValue();

    var year = dateFieldValue.getFullYear();

    var month = dateFieldValue.getMonth();

    var day = dateFieldValue.getDate();

    new Date(year,month,day);

    May I know how can I get exact date using JavaScript which I am selecting in DateTime control?

    Thanks,

    Mohit

  • PZ123 Profile Picture
    PZ123 on at
    RE: Getting only Date from Date time field using javascript

    Thank you Sir! You have saved the day!

  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Getting only Date from Date time field using javascript

    because you are concatenating startDate and not dateFormat

    the line should be

    var concatedField = field1 + "-" + field2 + "-" + dateFormat;

  • PZ123 Profile Picture
    PZ123 on at
    RE: Getting only Date from Date time field using javascript

    Thank you for the quick response!  I really appreciate. Your reply caused the code not break and work but it still is not doing what I want it to do which is retrieve the date only from the date/time "new_dateauthorized" field and set the value as mm-dd-yyyy. When I step through the code it sets the values right but when I hit continue and the code finishes it show the field like so:

    name: field1-field2-Tue Jul 12 2016 00:00:00 GMT-0700 (Pacific Daylight Time)


    instead of: field1 - field2 - 7-12-2016


    using this code:

    var startDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();

    if (startDate != null) {

    var startDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();

    var year = startDate.getFullYear() + "";

    var month = (startDate.getMonth() + 1) + "";

    var day = startDate.getDate() + "";

    var dateFormat = month + "-" + day + "-" + year;

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

     

    ending with:

    var concatedField = field1 + "-" + field2 + "-" + startDate;

    Xrm.Page.getAttribute("new_name").setValue(concatedField);

    Xrm.Page.data.entity.save();


  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Getting only Date from Date time field using javascript

    maybe the field is empty, so the getValue result is null and you can't get the year.

    try adding a check like

    var startDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();

    if (startDate != null) {

       var year = startDate.getFullYear() + "";

       // rest of the code

  • PZ123 Profile Picture
    PZ123 on at
    RE: Getting only Date from Date time field using javascript

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

  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Getting only Date from Date time field using javascript

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting only Date from Date time field using javascript

    Why add + 1 for month?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting only Date from Date time field using javascript

    how to set time also?

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

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans