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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Get Week No. from a date

(0) ShareShare
ReportReport
Posted on by 10

Hi Everyone,

I am very new to Dynamics365 sales and also to this forum. Please be kind if this seems a very basic question to you

So my requirement is to extract the week number when an opportunity was modified. I already have a field 'Modified On' but couldn’t find ways to calculate the week number from the date. Any help is appreciated.

Many Thanks,

Manisha

I have the same question (0)
  • Suggested answer
    Nya Profile Picture
    29,064 on at

    Hi Manisha,

    You can create a custom field whose field type is calculated.

     pastedimage1626775713821v1.png

    And set the calculation as:

     pastedimage1626775719066v2.png

    Then you’ll see the result.

    pastedimage1626775731762v3.png

  • Manisha Choudhary Profile Picture
    10 on at

    Thank you so much for your response. However, I think I didn't phrase the question properly. I am looking to get the week number when the opportunity was last modified not the duration in weeks. So, for example, if I modify an opportunity today i.e modified date 20-July-2021 it should show 'Week 29', for date 26-July-2021 it should be 'Week 30' so on.

    Thanks,

    Manisha

  • Suggested answer
    Sergi Valero Profile Picture
    145 on at

    Hey Manisha Choudhary!

    With the knowledge I have, I would suggest you use a Javascript function that triggers whenever the field 'Modified On' is changed. (Not sure if this is the best approach for your requirement)

    Here is the function: 

    Date.prototype.getWeek = function() {
      var date = new Date(this.getTime());
      date.setHours(0, 0, 0, 0);
      // Thursday in current week decides the year.
      date.setDate(date.getDate()   3 - (date.getDay()   6) % 7);
      // January 4 is always in week 1.
      var week1 = new Date(date.getFullYear(), 0, 4);
      // Adjust to Thursday in week 1 and count number of weeks from date to week1.
      return 1   Math.round(((date.getTime() - week1.getTime()) / 86400000
                            - 3   (week1.getDay()   6) % 7) / 7);
    }
    
    function CalcWeekNumber (executionContext) {
        const formContext = executionContext.getFormContext();
        const modifiedDate = new Date(formContext.getAttribute('modifiedon').getValue());
        const weekNumber = modifiedDate.getWeek();
        
        formContext.getAttribute('weekNumberField').setValue(weekNumber);
    }

    Source of the function

    This function will set the ISO week number of the Modified on date to a new field you need to create. Make sure to change 'modifiedon' and 'weekNumberField' for the schema name of your field.

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 > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 141 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 137

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans