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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Date Time format - JavaScript

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Deepesh161 Profile Picture
    6,317 on at

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

  • Community Member Profile Picture
    on at

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

  • Suggested answer
    nghieppham Profile Picture
    4,755 on at

    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
    on at

    This worked perfectly! Thank you very much!!

  • Suggested answer
    FidelMartin Profile Picture
    128 on at
    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;
    
     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans