Wrong time being returned by date-fns when involving 2 timezones - vue.js

Introduction
I have the current situation:
I receive the date from the backend as UTC
The user can have in his own profile his timezone setup
User can pick the date and time in two different time pickers
As soon as the date is returned by the backend I convert it to the user local timezone (timezone of his profile)
User selects new date, I create a zonedDate, modify it and store it as UTC
User selects new time, I use the utcToZonedTime to convert to user's local timezone (the UTC returned from backend), then I update with setHours and setMinutes and store back as UTC using toISOString() string
How the code is structured
I currently have the following code for when the user pick the date
import utcToZonedTime from 'date-fns-tz/utcToZonedTime';
const localizedTime = computed(() =>
utcToZonedTime(record.value.date, loggedUser.timezone)
);
const date = computed({
get: () => formatToDate(localizedTime.value),
set: (value: string) => {
// The `value` will come as YYYY-MM-DD from the DatePicker component
const zonedDateString = `${value}T17:30`;
const zonedDate = toDate(zonedDateString, {
timeZone: loggedUser.timezone,
});
record.value.date = zonedDate.toISOString();
},
});
This works fine for now. I set the hour and minutes to a hardcoded 17:30.
Now, the problem happens whenever picks the time:
import utcToZonedTime from 'date-fns-tz/utcToZonedTime';
import setMinutes from 'date-fns/setMinutes';
import setHours from 'date-fns/setHours';
const localizedTime = computed(() =>
utcToZonedTime(record.value.date, loggedUser.timezone)
);
const time = computed({
get: () => format(localizedTime.value, 'HH:mm'),
set: (time: string) => {
// Time is returned as HH:mm from the input
const [hour, minutes] = time.split(':');
console.log('Localized time:')
console.log(localizedTime.value);
let currentDateParsed = setHours(
localizedTime.value,
hour as unknown as number
);
currentDateParsed = setMinutes(
currentDateParsed,
minutes as unknown as number
);
console.log('Current date parsed:')
console.log(currentDateParsed);
record.value.date = currentDateParsed.toISOString();
},
});
Problem
As a setup I have the following criterias:
User timezone is set to Europe/Lisbon (GMT+1)
My computer timezone is set to Europe/Zagreb (GMT+2)
I receive the following date from backend in UTC 2022-06-02T16:00:56.000000Z
Time is displayed as 17:00 (I'm using the user profile's timezone and not the computer's timezone)
Whenever I set the date, it works perfect time. Whenever I try to set hours and minutes, the timezones get mixed up.
I select the time as 18:30 and it is displayed back to the user as 17:30.
The part of the code that is giving me problems is:
const time = computed({
get: () => format(localizedTime.value, 'HH:mm'),
set: (time: string) => {
// Time is returned as HH:mm from the input
const [hour, minutes] = time.split(':');
console.log('Localized time:')
console.log(localizedTime.value); 👈 // Thu Jun 02 2022 17:00:56 GMT+0200 (Central European Summer Time)
let currentDateParsed = setHours(
localizedTime.value,
hour as unknown as number
);
currentDateParsed = setMinutes(
currentDateParsed,
minutes as unknown as number
);
console.log('Current date parsed:')
console.log(currentDateParsed); 👈 // Thu Jun 02 2022 18:30:56 GMT+0200 (Central European Summer Time)
console.log('Current date parsed toISOString():');
console.log(currentDateParsed.toISOString()); 👈 // 2022-06-02T16:00:56.000Z
record.value.date = currentDateParsed.toISOString();
},
});
I have tried converting the date back to UTC and work only in UTC and also only in user's local time but nothing seems to work. I have uploaded the following video showing the issue

Related

How to convert date and obtain new date with timezone in composiiton api by using #nuxtjs/dayjs?

I use #nuxtjs/dayjs in my nuxt-composiiton api project . I need to convert my date and i neeed to obtain new date with timezone
2022-04-11T15:13:00.000Z ----> 2022-04-11T15:13:00+03:00
const { $dayjs } = useContext();
const date='2022-04-11T15:13:00.000Z'
dayjs usage in composition api => $dayjs(date).format('') .....
https://day.js.org/docs/en/parse/string-format
console.log(' $dayjs ', $dayjs(date).format('YYYY-MM-DDTHH:mm:ss Z'));

How to format the date and time from a date time picker

I am currently working on a reminders app and I added a DateTimePickerModal from the package react-native-modal-datetime-picker and I wanted to know how to format the data...
This is the code for the component
<DateTimePickerModal
isVisible={this.state.modalVisible}
mode="datetime"
onConfirm={onConfirm}
onCancel={onCancel}
/>
Code for the onConfirm function
const onConfirm = (dateAndTime) => {
this.setState({ dateAndTime: dateAndTime.toString() });
hideModal();
};
The data I'm getting:
Converted into string: Mon May 03 2021 16:01:37 GMT+0530 (IST)
Without converting it to string: 2021-05-06T09:31:37.000Z
If you have any idea on how to do this please let me know...Thanks in advance!
Yes you are on the right way
Just create a function to format Date like this
const FormatDate = (data) => {
let dateTimeString =
data.getDate() +
'-' +
(data.getMonth() + 1) +
'-' +
data.getFullYear() +
' ' +
data.getHours() +
':' +
data.getMinutes();
return dateTimeString; // It will look something like this 3-5-2021 16:23
};
Then for setting state do like this
this.setState({ dateAndTime: FormatDate(dateAndTime) });
Here is a Snack to see a working example. It will run on iOS and Android only as
DateTimePicker is not supported on: web

expo notification how set start time

The challenge is: how can I set up the start date for notification in the new expo API. In the old API (which is depricated today) it was feasible but I can't see the solution in the new doc
For example: set up a notification on every 2nd day start from a user given date (e.g. 12th of Jan).
I went through on these types
export type SchedulableNotificationTriggerInput =
| DateTriggerInput
| TimeIntervalTriggerInput
| DailyTriggerInput
| WeeklyTriggerInput
| CalendarTriggerInput;
but no success so far.
As far as I can tell you should be able to set the start date this way:
const dateString = "21/01/2021"; // Jan 21
const trigger = new Date(dateString);
trigger.setMinutes(0);
trigger.setSeconds(0);
Notifications.scheduleNotificationAsync({
content: {
title: 'Happy new hour!',
},
trigger,
});
You should play around with trigger.setMinutes and trigger.setSeconds to set a specific hour on your specific day.

Postman: Check date equals yesterdays date

I'm running a GET request using Postman and the JSON response is below:
{
"Version": "1.0.524.0",
"BuildName": "Live",
"BuildDate": "04/12/2018 18:58:50.85 \r\n"
}
The build date increments by one each day and I want to write a script to check the date has been incremented and if it hasn't then its a failed test.
Currently I have this:
pm.test("Check Build Date", function (CheckDate) {
pm.expect(pm.response.text()).to.include("04/12/2018 18:58:50.85");
});
However I know if I run this script tomorrow it won't work and I will get the following error:
FAIL Check Build Date | AssertionError: expected '{"Version":"1.0.524.0","BuildName":"Live","BuildDate":"05/12/2018 18:58:50.85 \\r\\n"}' to include '04/12/2018 18:58:50.85'
Can anybody assist, please?
If your response body is an object, like in your question, you can use pm.response.json().BuildDate and check that it equals that value.
pm.test("Check Build Date", () => {
pm.expect(pm.response.json().BuildDate).to.equal("04/12/2018 18:58:50.85 \r\n");
});
Not sure why you have those \r\n values in string on that response, they seem in the wrong place to me.
To know that the day has incremented by 1, you would need to store the state of the last run and do a check against that. You could be that with setting it as an environment variable.
Send an initial request to get and set the BuildDate. You will now have that value to use in a Check request.
pm.environemnt.set('lastBuildDate', pm.response.json().BuildDate)
In the check request, you could have something like this:
const moment = require('moment')
let dateDiff = moment(pm.reponse.json().BuildDate).isAfter(pm.environment.get('lastBuildDate'))
pm.test("Check Build Date", () => {
pm.expect(dateDiff).to.be.true
})
If you just want something that checks if the BuildDate is the after the time that that you are making the request - You could use something like this:
const moment = require('moment')
let dateNow = moment().format("DD/MM/YYYY hh:mm:ss.SS")
let dateDiff = moment(pm.response.json().BuildDate).isAfter(dateNow)
pm.test("Check Build Date", () => {
pm.expect(dateDiff).to.be.true
})
This is obviously not what you want to do but I would suggest taking a look at the momentjs module as see if that is something in there that you can use for your use case.

Exchange Api send incorrect invitation in Israel timezone

I faced to following problem with sending invitation in .net using exchange API
Send invitation in Israel timezone with start time =09/09/2013 4.30 Israel.
But it is displayed in outlook 09/09/2013 6.30 Israel.
It works properly for other time zones for example for EST.
Does anybody has any idea how to fix this?
Sample:
service.AutodiscoverUrl(loginForm.tbEmailAddress.Text, f);
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Israel Standard Time");
Appointment appointment = new Appointment(service);
appointment.Subject = "subj";
appointment.Body = new MessageBody(BodyType.Text, "body");
appointment.StartTimeZone = timeZoneInfo;
appointment.Start = GetDateTime(new DateTime(2013, 09, 09, 04, 0, 0));
appointment.EndTimeZone = timeZoneInfo;
appointment.End = GetDateTime(new DateTime(2013, 09, 09, 04, 30, 0));
appointment.IsAllDayEvent = false;
appointment.Importance = Importance.Normal;
appointment.RequiredAttendees.Add("Lena", "email...");
appointment.Save(SendInvitationsMode.SendOnlyToAll);
...
private static DateTime GetDateTime(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute,
dateTime.Second, dateTime.Millisecond, DateTimeKind.Unspecified
);
}
I use Microsoft.Exchange.WebServices.dll 15.0.516.14
it's actually a known issue with Exchange EWS itself. Developer can only trust the date time returned without the timezone information, and the correct timezone for that time should be UTC always.