Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Date Time format - JavaScript

Posted on by Microsoft Employee

Hi -

I have a datetime field which I'm using in a JavaScript function:

function getDate(){
var date = Xrm.Page.getAttribute("new_startdate").getValue();
alert (date)
}

The alert displays the the format of the field as this: Tues Apr 19 08:00:00 UTC+0100 2016

I need to reformat the result to show as this: 2016-04-19 08:00:00, so I can use it in a further part of the function.

Does anyone know how to do this in the JavaScript function?

Thanks,

Rich

*This post is locked for comments

  • Suggested answer
    FidelMartin Profile Picture
    FidelMartin 128 on at
    Date Time format - JavaScript
    Hello User,
     
    Follow below code to format Date & Time
     
    var dateObject = new Date("Tues Apr 19 08: 00: 00 UTC + 0100 2016");
    
                var year = dateObject.getFullYear();
                var month = dateObject.getMonth() + 1;
                var day = dateObject.getDate();
                var hours = dateObject.getHours();
                var minutes = dateObject.getMinutes();
                var seconds = dateObject.getSeconds();
    
                if (month < 10) {
                    month = '0' + month;
                }
                if (day < 10) {
                    day = "0" + day;
                }
                if (hours < 10) {
                    hours = '0' + hours;
                }
                if (minutes < 10) {
                    minutes = '0' + minutes;
                }
                if (seconds < 10) {
                    seconds = '0' + seconds;
                }
    
                var formattedDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    
     
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Date Time format - JavaScript

    This worked perfectly! Thank you very much!!

  • Suggested answer
    nghieppham Profile Picture
    nghieppham 4,755 on at
    RE: Date Time format - JavaScript

    Hi Rich,

    You have to convert UTC Time to you local time, please check this link for more information.

    praveenlobo.com/.../how-to-convert-javascript-local-date-to-utc-and-utc-to-local-date

    Regards,

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Date Time format - JavaScript

    Thank you - which one of these methods should I use??

  • Suggested answer
    Deepesh161 Profile Picture
    Deepesh161 6,317 on at
    RE: Date Time format - JavaScript

    www.elated.com/.../working-with-dates

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans