Introduction
While designing applications, we may come across multiple scenarios where our application could run across multiple locations and timezones, and working with all these timezones could be a difficult task.
With the introduction of SharePoint framework, creating a scalable and UI friendly application is very easy. With modern SharePoint, creating an Intelligent intranet is very easy and is becoming popular. There can be a scenario where intranet users are across multiple locations, therefore we need to create an SPFx solution considering all these locations.
We will use Moment Timezone to get all the timezones and work with conversions of the timezone. To get the current user’s timezone, we will use jstz to fetch the current users' timezone. Before diving into the SPFx solution, let us understand how SharePoint works with timezones out of the box
How SharePoint stores date and time field
Out of the box, SharePoint Converts the date and time field in the regional setting of the site.
To understand this let's see an example:
- Let's create a sample list name Learning DateTime.
- Add a new field named SampleDateTime where we would also store time values.
- Check the regional setting of the site collection to check that navigate to site setting and select the regional setting, as displayed in the image below:

- Check the regional setting time zone value. So, if a User say for example is creating a new item in the list, the date and time value will be converted as per the regional setting and would be stored in the ISO format in the list.

- Let's create a new item in the list and check what is the value that is being stored in the list. As displayed in the image below, we have selected 8 am as the time, but when we fetch the data, it provides the time as (15), which is 3 pm. This means it has added 7 hours to our selected time, which is because of the timezone difference.


How to store data in SharePoint List without conversion
To store the date-time without converting to site collection regional setting, we can pass the date and time in the iso format using the REST API and this will not convert our entered data in the regional setting
- URL:- https://testinglala.sharepoint.com/sites/Test/_api/web/lists/getbytitle('Learning datetime')/items?$select=Title,SampleDateTime
- Data:- {Title:'Test POST',SampleDateTime:"2020-09-20T15:30:00Z","__metadata": {"type": "SP.Data.Learning_x0020_datetimeListItem"} }
Create an SPFx Solution to handle different timezone
To create an SPFx solution, let's run the below command and follow the steps as displayed in the below image.
- mkdir timezones
- cd timezones
- yo @microsoft/sharepoint

We will also add the required library which would be helpful for our solution and understanding. Use the below command to add those:
- PnP js
- PnP reusable controls
- moment timezone
- jstz
npm i @pnp/js @pnp/spfx-controls-react moment-timezone jstz - -save - -save-exact
Now, let's create a dropdown that would display all the Timezones and users can select the timezone of their choice. To do so, let's update the MultipleTimezone.tsx file.
Add the below import statement in the file at the top of the file along with all the imports.
- import { PrimaryButton, values } from 'office-ui-fabric-react';
- import { Dropdown, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';
- import { DateTimePicker, DateConvention, TimeConvention, TimeDisplayControlType } from '@pnp/spfx-controls-react/lib/dateTimePicker';
- import * as moment from "moment-timezone";
- import * as jstz from "jstz";
- import { sp } from "@pnp/sp";
- import "@pnp/sp/webs";
- import "@pnp/sp/lists";
- import "@pnp/sp/items";
Now we will use moment.tz.names() which provides us all the timezone names. We will manipulate it to get the offset for all the time zones. Add the below code in the next line after all the import statements are completed.
- const selectorOptions = moment.tz.names().reduce((memo, tz) => {
- memo.push({
- name: tz,
- offset: moment.tz(tz).utcOffset()
- });
- return memo;
- }, []).sort((a, b) => {
- return a.offset - b.offset;
- }).reduce((memo, tz) => {
- const timezone = tz.offset ? moment.tz(tz.name).format('Z') : '';
- return memo.concat(`{"key":"${tz.name}", "text":"(GMT${timezone}) ${tz.name}"},`);
- }, "");
- const timeZoneOptions = JSON.parse(`[${selectorOptions.substring(0, selectorOptions.length - 1)}]`);
As we can see from the above code, we created a JSON object of key and text pair which is compatible with office UI fabric dropdown so we will use the timeZoneOptions variable for all the dropdown options. Now to add the actual office UI fabric dropdown, let's remove the HTML provided in the render method and replace it with the below code.


Bhargava ChPosted May 23, 2021, 8:50 AM
Moment is not taking internally and I checked this solution it is not taking daylight bias please suggest me for next steps
Bhargava ChPosted Sep 24, 2020, 2:06 AM
It is not working for daylight saving hours.