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)

Javascript to add one hour to NOW in CRM

(0) ShareShare
ReportReport
Posted on by

I want to take Date.now and add 1 hour.  This is my script:

var now2 = Date();
now2.setHours(now.getHours() + 1);
Xrm.Page.getAttribute("new_todaysdate").setValue(now2);

When it executes, page says there is no function for getHours.  HUH?  I thought that's supported JS.

So then i tried this:

// From: stackoverflow.com/.../how-to-add-30-minutes-to-a-javascript-date-object
var now = Date.now();
var now2 = new Date(now.getTime() + 60 * 60000);
Xrm.Page.getAttribute("new_todaysdate").setValue(now2);

Same type of error.  Says getTime is not a function.  Again, this says it is: http://www.w3schools.com/jsref/jsref_gettime.asp

What am i doing wrong?

Thank you

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    tw0sh3ds Profile Picture
    5,600 on at

    Hi,

    you should do

    var now = new Date();

    and it will all work.

    When you are doing Date() or Date.now() you are not getting an object of Date, you are getting a string with a date, that;s why you are getting error (because string does not have the functions you want, Date object has them)

  • Daniel at VNS Profile Picture
    on at

    Thanks Pawel.  Yes, that's what I have been doing and that generates the exact moment the form executes.  I need to add 1 hour to that number.    The form is used to determine when the Case is overdue and since CRM does not have any 'what time is it NOW' functionality OOB, this is what we need to do to work around it.  However, if a user makes a change even 1 second later, then 'now' is 'overdue' and we have a problem.  So i want to add 1 hour to now to give the user time to actually update the record.

    I think i understand the change you've suggested.  Will try it 'now'  :)

  • Daniel at VNS Profile Picture
    on at

    Same result.  system tells me that getTime is not a function.  Can't understand what I've done wrong.

       var now = Date();

       var now2 = new Date(now.getTime() + 60 * 60000);

       Xrm.Page.getAttribute("new_todaysdate").setValue(now2);

  • Verified answer
    tw0sh3ds Profile Picture
    5,600 on at

    Hi,

    You did not get my answer. You did not use "new". You should do:

    var now = new Date();

      var now2 = new Date(now.getTime() + 60 * 60000);

      Xrm.Page.getAttribute("new_todaysdate").setValue(now2);

  • Verified answer
    tw0sh3ds Profile Picture
    5,600 on at

    Also the solution with setHours and getHours was a lot better, so I would also do this as in your first version of code (just use "new")

  • Daniel at VNS Profile Picture
    on at

    I *completely* missed that.  You're right - that did the trick.

    I'm normally very good with details but learning JS is testing the limits of my attention to detail.

    Thank you again Pawel.

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