0

I have a site with a calendar. In the calendar I have a mix of recurring and non-recurring events.

On a WebPart page I have a CEWP that uses WebServices (_vti_bin/Lists.asmx?op=GetListItems) through JavaScript to get a list of those events for a specific month (a la https://sharepoint.stackexchange.com/questions/160778/daterangesoverlap-to-show-events-for-last-month-in-a-dvwp). So I have a list of dates like so:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="..." xmlns:xsi="..." xmlns:xsd="...">
    <soap:Body>
        <GetListItemsResponse xmlns="...">
            <GetListItemsResult>
                <listitems xmlns:s='...' xmlns:dt='...' xmlns:rs='...' xmlns:z='...'>
                    <rs:data ItemCount="1">
                        <z:row
                            ows_Title='recurring meeting one'
                            ows_EventDate='2016-01-26 11:00:00'
                            ows_EndDate='2016-01-26 12:00:00'
                            ows_Description='...'
                            ows_fRecurrence='1'
                            ows_RecurrenceData='...'
                            ows_fAllDayEvent='0'
                            ows__ModerationStatus='0'
                            ows__Level='1'
                            ows_ID='2.0.2016-01-26T16:00:00Z'
                            ows_UniqueId='...'
                            ows_owshiddenversion='2'
                            ows_FSObjType='2;#0'
                            ows_Created='2015-10-22 08:34:47'
                            ows_PermMask='0x7fffffffffffffff'
                            ows_Modified='2015-10-22 09:22:05'
                            ows_FileRef='2;#/xxx/Lists/Meeting Calendar/2_.000'
                            ows_MetaInfo='2;#' />
                    </rs:data>
                </listitems>
            </GetListItemsResult>
        </GetListItemsResponse>
    </soap:Body>
</soap:Envelope>

What I'm trying to do now is iterate over each event to see if it is occurring right now.

I could use new Date() to get the date now, but that would be for the client's timezone/settings. The SharePoint site may use a different regional setting. And, the user's regional settings may inherit from the site or the user may have their own regional setting -- both of which would effect the output from the GetListItems call.

Ideally I'd love a way to get the current time accounting for, in order:

  • the user's regional settings
  • the site's regional settings if the user is inheriting from the site

Any thoughts/ideas?

1 Answer 1

1

I was able to do what I needed by using the ows_ID attribute of the event. It has the start time in UTC in it; something like 3.0.2015-11-25T22:00:00Z. The ows_EventDate has the time taking into account all the regional settings. Using some simple math I was able to do what I needed.

// get the ID (e.g. 3.0.2015-11-25T22:00:00Z), split to get the start time part, remove the T and Z and parse into a Date object
var eventStartDateUTC = new Date(rows[i].getAttribute("ows_ID").split(/\./g)[2].slice(0,-1).replace(/T/, " ").replace(/-/g, "/")).getTime();

// get the start time after all regional settings are applied
var eventStartDate = new Date(rows[i].getAttribute("ows_EventDate").replace(/-/g, "/")).getTime();

// find the UTC offset
var serverUTCOffset = eventStartDate - eventStartDateUTC;

// get the end time after all regional settings are applied
var eventEndDate = new Date(rows[i].getAttribute("ows_EndDate").replace(/-/g, "/")).getTime();

// find the end time in UTC using the previously calculated offset
var eventEndDateUTC = eventEndDate - serverUTCOffset;

// get the time now
var now = new Date();

// calculate the offset to get the UTC time
now = now.getTime() + (now.getTimezoneOffset() * 60 * 1000);

if(now >= eventStartDateUTC && now <= eventEndDateUTC) 
{
    // active event
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.