How to convert UTC timezone to CET timezone in graph api? - microsoft-graph-api

Am tried with this piece of code
var Option = new List<Option>()
{
new QueryOption("startDateTime", filterStartDate.ToLongDateString()),
new QueryOption("endDateTime", filterEndDate.ToLongDateString()),
new HeaderOption("Prefer","outlook.timezone=\"Central European Time\"")
};
but am getting the bad request error.
Can anyone help me with this?

I have 2 ideas on your scenario, the first is that, according to the official sample, you can add Prefer request header like this if you used Graph SDK.
var events = await graphClient.Me.Events
.Request()
.Header("Prefer","outlook.timezone=\"Pacific Standard Time\"")
.Select("subject,body,bodyPreview,organizer,attendees,start,end,location")
.GetAsync();
By the way, the time zone you used is Central European Time but it's not supported by graph api. Per my searching, only Central European Standard Time can be found.
In general, the timeZone property can be set to any of the time zones currently supported by Windows, as well as the additional time zones supported by the calendar API.

The correct time zone is Central Europe Standard Time not Central European Time.
If you are calling calendar view events, startDateTime and endDateTime must be represented in ISO 8601 format.
var queryOptions = new List<QueryOption>()
{
new QueryOption("startDateTime", filterStartDate.ToUniversalTime().ToString("o")),
new QueryOption("endDateTime", filterEndDate.ToUniversalTime().ToString("o"))
new HeaderOption("Prefer","outlook.timezone=\"Central Europe Standard Time\"")
};
var calendarView = await graphClient.Me.Calendar.CalendarView
.Request(queryOptions)
.GetAsync();

Related

Microsoft graph delta query documentation clarification

I am going through documentation for Microsoft Graph https://learn.microsoft.com/en-us/graph/delta-query-events?tabs=java and found that section "The next round: sample first request" doesn't include delta query as part of request in which case the current state meaning all the events should be returned.
Also, I see that the same request has no start or end date. I am aware that this call is being made using the deltaToken received in last call. So, should start and end Date be passed while making a call where I am using deltaToken to fetch the next set of changes?
Below is the code used in Documentation(Neither deltaToken is included nor Start and End date):
IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
LinkedList<Option> requestOptions = new LinkedList<Option>();
requestOptions.add(new HeaderOption("Prefer", "odata.maxpagesize=2"));
IEventDeltaCollectionPage delta = graphClient.me().calendarView()
.delta()
.buildRequest( requestOptions )
.get();
Please help. Thanks in advance.
Any query parameter provided in the initial Delta query will be encoded in the Delta or skip token, this is why you don't need to pass it every time. (Start and end dates)
There seems to be an issue with this documentation page, it's never showing to use the Delta link and passing it back as a Delta token query parameter (similar to what's done with the skiptoken). I'll PR the docs tomorrow to get that fixed.

Can't fetch supported time zones in Iana format

Trying to fetch supported time zones in Iana format from MS Graph using this code
var timeZones = await graphServiceClient
.Me
.Outlook
.SupportedTimeZones(TimeZoneStandard.Iana)
.Request()
.GetAsync();
I get this error
The parameter 'TimeZoneStandard=Iana' is not in scope.
Making the same call without the TimeZoneStandard.Iana parameter succceeds. Is TimeZoneStandard.Iana not supported?
Parameter needs to be correctly given as
(microsoft.graph.timeZoneStandard'Iana') as shown in below image.
Please check this image
Please refer this document for better understanding.

microsoft graph createreply wrong timezone

In our Angular 5 application, we are using Microsoft Graph to retrieve mail messages from a mailbox and then send a reply. For creating the reply, we use the REST API:
https://graph.microsoft.com/v1.0/me/messages/{message ID}/createReply
This creates a reply with a timezone that is UTC, but we expect it to be GMT+1. For example, in the mail body it says:
From: Melissa van Dijk
Sent: Friday, February 23, 2018 9:51:49 AM (wrong timestamp)
To: Melissa van Dijk
Subject: Meet for lunch?
We checked the settings in our Office 365 mail account and there it is specified that our local timezone is GMT+1 (Brussels, Amsterdam...).
(When replying via Outlook webmail, we get a correct timestamp).
Is this a bug or do we have to correct this timestamp ourselves? Or do we need to pass the timezone with the REST call somehow?
Thanks in advance!
I'm not sure if you can adjust the human-formatted timestamps in the HTML message body, but you can certainly use the ISO 8601 formatted timestamps in the other fields in the JSON of the response. For example, you'll find:
{
...
"sentDateTime": "2018-02-23T09:51:49Z",
...
}
While this is also in UTC (denoted by the Z), you can easily parse it by using a JavaScript Date object, or Angular's own datetime functions, or your favorite time library such as Luxon, Moment, or Date-fns. From there, displaying it in local time is trivial.

Apply DateTime filter for getting list messages in gmail using gmail API

I am using GMAIL API to get messages between a date range. My problem is for one of my client account the display datetime and actual received date/sent datetime differs. IS there a way to read messages in UTC time zone?
Thanks,
Haseena
There is an open issue that affects the timezone of dates in search queries. Unfortunately there is no workaround at the moment.
$newTime = strtotime('-15 day');
$after = strtotime(date('Y-m-d H:i:s', $newTime));
$opt_param = array();
$userId = 'me';
$opt_param['q'] = "after:$after";
$messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);

Prevent JsonResult from auto-formatting Dates

I'm writing a cloud based program that utilizes UTC for date saving to the server, and convert it back on the round trip. Problem is, my instance in the cloud is autoconverting the JsonResult datetime values according to the localization settings on the browser obtaining the result.
I have gone through tons of steps to make sure the server side code on both levels is returning the proper data, and it is on all instances, and no Javascript code on my page is making the change either (even removed all my Javascript date formatting just in case), I have traced out with Firebug to determine the exact point that it changes, is when my page receives the Json Result from my JsonResult method. Like I said, I have debugged the values before being sent to the page, and they are proper, and on my response, they are modified based upon browser location....
Has anyone had a similar problem?
Value returned:Date(1341792000000) 07/08/2012 17:00
Should be: Date(1341817200000) (07/09/2012 12:00AM)
Thanks
I finally ended up getting the proper results, with many modifications to my application. I did a lot of stuff to make this happen... First, I implemented timezone.JS to get a listing of timezones that will be used within the application, and used jstz to get current timezone of browser loading the page. Next, I have to make (for mvc) an file get method that accesses the timezones to load into timezoneJS.
Next, on save of the timezone, I specified pst as the type, and then convert back to utc on roundtrip to update the interface.
On formatting of my Json date, I run the timezoneJS method and get the timezone name from jstz, and set the new date value like such:
var timezone = jstz.determine();
timezoneJS.timezone.zoneFileBasePath = '/Item/GetTz'; // get file method
var dt = new timezoneJS.Date(parseInt(jsonDate.substr(6), timezone.name())); // strips out date from json date
dt.setTimezone('America/Los_Angeles');
This allows on the cloud projects to be ran on any server, and displayed in any browser regardless of timezone, and allow the user to view and configure timezone sensitive data natively, and allow for users to see the start/end date of configurable database values.
Maybe using http://msdn.microsoft.com/en-us/library/system.datetime.specifykind.aspx
Did you try with,
date.toLocaleString()
Alternatively,
You can create a new Date object and use Date.setUTC
As the OP said:
Problem is, my instance in the cloud is autoconverting the JsonResult datetime values according to the localization settings on the browser obtaining the result.
Recently experienced something similar. Strange behavior coming from $.ajax response. Depending on the language setting of the browser, a date received in string format gets converted into whatever is set in the language setting.
For example, in Postman, Web API response is like so:
{
"id": "10057",
"d_date": "3/30/2017 3:00:00 AM",
"sum": 253.0
},
If the browser is set to english(en/en-us), response is the same as above.
If the browser is set to english-uk(en-gb), response becomes:
{
"id": "10057",
"d_date": "30/03/2017 03:00:00",
"sum": 253
}
If the browser is set to german(de), response becomes:
{
"id": "10057",
"d_date": "30.03.2017 03:00:00",
"sum": 253
}
So somehow, the browser or the $.ajax library is trying to be smart and formatting the dates automatically.
Probably, the best solution is that the Web API send the date in ISO date time format.
Another solution if you can't change the backend is to add accept-language: en in the AJAX header request. Something like so:
$.ajax({
type: 'POST',
url: '/endpoint/',
contentType: 'application/json',
data: JSON.stringify(body),
dataType: 'json',
async: false,
headers: {
'accept-language': 'en'
}
})

Resources