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)

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

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