StratusForms icon indicating copy to clipboard operation
StratusForms copied to clipboard

Datefields UTC issue

Open btroop202 opened this issue 6 years ago • 15 comments

OK been hacking at this for a few days now. What is the best way to handel dats with the server in a different timezone then the clients. I need to make sure the dates work, right now I have it working in the morning, but in the afternoon it adds a day.

I am running SP 2013

btroop202 avatar Oct 01 '18 14:10 btroop202

I LOATHE Timezones... best approach I've found to date is to use moment.js you can easily create/get a UTC date. I thought about throwing a dependency on StratusForms for moment.js to help but I hate having so many dependencies as it is.

mrackley avatar Oct 01 '18 15:10 mrackley

Your server time is my guess is running 12 hours behind. I. Think it works on regional time settings of the site. When somebody of a different timezone adds an entry they should see their entry in their timezone...

Date format must be yyyy-mm-dd hh:mm:ss (yeah! With seconds!)

This is particularly important if you are using datetime pickers...

On Tue., 2 Oct. 2018, 12:45 am Darrin, [email protected] wrote:

OK been hacking at this for a few days now. What is the best way to handel dats with the server in a different timezone then the clients. I need to make sure the dates work, right now I have it working in the morning, but in the afternoon it adds a day.

I am running SP 2013

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mrackley/StratusForms/issues/33, or mute the thread https://github.com/notifications/unsubscribe-auth/AE22I7UhMt203nDG8ESdzBwJ0rd3xATIks5ugiqLgaJpZM4XCOUV .

davidcfk avatar Oct 01 '18 16:10 davidcfk

So here is where I am getting stuck I have for inputs: Start : '10/24/2018' End : '11/08/2018'

The function below returns the following: Start: Tue, 23 Oct 2018 23:00:00 UTC End: Thu, 8 Nov 2018 00:00:00 UTC

I am not understanding why the time is not the same, I would think they would be the same? SO when I save the start is now the 23 Oct and not 24 Oct.

function fixDatesUTC(dateX){
		var Year = dateX.substring(6, 10);
		var Month = dateX.substring(0, 2);
		var Day = dateX.substring(3, 5);
		var NewDate = new Date(Year, (Month-1), (Number(Day)));
		return NewDate.toUTCString();
};

btroop202 avatar Oct 02 '18 08:10 btroop202

No mater what I send to the server for the start and end it stores:

Start: 2018-10-23 23:00:00 End: 2018-11-08 00:00:00

I send this to the server:

Start: Wed, 24 Oct 2018 05:00:00 UTC End: Thu, 8 Nov 2018 06:00:00 UTC

I just do not know what to send to get the date to stay right? Any ideas? The server time is one hour behind me from my location

btroop202 avatar Oct 02 '18 10:10 btroop202

Wait up... did you declare the field to be a DateTime? Change it just to date only... by default, Sharepoint reckons all appointments take an hour to do...

As for sending dates into Sharepoint, this is my format: 'Y-m-d H:i:s' (That's 2018-10-23 05:00:00) only. Your timezone will take your system's timezone, sharepoint will make that conversion (I believe!)

As for getting the formats back, after you ask SP to spit out a date, without using MOment, you can format date objects to ISO format (I don't know if you prefer to use UTC or not) -- ... but new Date([your date]).toISOString() generally gets you a string format that's going to work. But do not use that ISO string as input back to SP, it will fail.

davidcfk avatar Oct 02 '18 10:10 davidcfk

moment.js

btroop202 avatar Oct 02 '18 11:10 btroop202

That is a issue, since it is a calendar web app, the EventDate and EndDate are set and I can not edit them

btroop202 avatar Oct 02 '18 11:10 btroop202

I have downloaded moment.js, how will this help solve this issue?

btroop202 avatar Oct 02 '18 11:10 btroop202

If anyone has a js function that handels this, PLEASE POST.

btroop202 avatar Oct 02 '18 11:10 btroop202

I'll have a quick look tomorrow... Maybe even remote into a machine that isn't in here as a test.

davidcfk avatar Oct 02 '18 12:10 davidcfk

Thanks, for now what I have to do is add 1 day to the start date, but not to the end date....which is odd that i do not get the same result for both dates.

btroop202 avatar Oct 02 '18 13:10 btroop202

You know... You are getting a result a day behind because you are one hour behind server time, with your new Date instantiate creates a time 00:00:00 without instructions, SP may well be correct. So, please include time in your declaration).

You might find it resolves your clients case as well.

davidcfk avatar Oct 02 '18 17:10 davidcfk

I tried that as well, I started with the offset added to the hour, then I just set the hour to 1pm. No matter what time I send, SP stores it as 2018-10-23 23:00:00. but only for the start date, then end date is 2018-10-25 00:00:00, which is the right date.

Its just odd as there is no constant

btroop202 avatar Oct 03 '18 07:10 btroop202

Can you show me the new date code you are using?

On Wed., 3 Oct. 2018, 5:43 pm Darrin, [email protected] wrote:

I tried that as well, I started with the offset added to the hour, then I just set the hour to 1pm. No matter what time I send, SP stores it as 2018-10-23 23:00:00. but only for the start date, then end date is 2018-10-25 00:00:00, which is the right date.

Its just odd as there is no constant

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/mrackley/StratusForms/issues/33#issuecomment-426541881, or mute the thread https://github.com/notifications/unsubscribe-auth/AE22I10i50cggx1MWI9K3Vd-GobMxg9cks5uhGqqgaJpZM4XCOUV .

davidcfk avatar Oct 03 '18 09:10 davidcfk

sorry been fighting another issue, have not cricled back to this yet...but will need too

here is the code I am using // To adjust for UTC issues with SP var UTCDate = fixDatesUTC($('#EventDate').val(),1); $('#EventDate').val(UTCDate);

		var UTCDate = fixDatesUTC($('#EndDate').val(),0);
		$('#EndDate').val(UTCDate);

function fixDatesUTC(dateX, dateAdd){

		var Year = dateX.substring(6, 10);
		var Month = dateX.substring(0, 2);
		var Day = dateX.substring(3, 5);
		//var hour = '00';
	
		var tDate = new Date(Year, (Month-1), (Number(Day)+dateAdd));
		//var utcOffsetMinutes = tDate.getTimezoneOffset();	
		//var NewDate = new Date(tDate.getTime());
		//NewDate.setTime(NewDate.getTime() - (utcOffsetMinutes*60*1000));
		//var dateToStart = new Date(NewDate);
					
		var dateToStart = moment(tDate).format("YYYY-MM-DDT07:00:00.000") + "Z";			
		// window.console.log(dateToStart);			
		return dateToStart;
};

btroop202 avatar Oct 12 '18 10:10 btroop202