I am running into an issue where CRM doesn't appear to be treating the DateTime correctly with OData. Specifically, we are trying to create a custom record called new_vod with a start and end time (and a few other attributes that I have removed from here for simplicity) using a JavaScript call to the CrmRestKit. When the user performing this function is not in the same time zone as the server, the time is incorrect by whatever the offset between the server and the user happens to be.
For example, if my CRM user is in Mountain Time and the Server is in Eastern Time, the Start Time will be 2 hours from now (even though it should be now) when looking at the newly created record in CRM. The user's computer's timezone doesn't seem to have any impact on the result. (I'm comparing the "new_starttime" against the "Created On" time to confirm I'm not just viewing the data wrong, and the created on time is 2 hours before the start time in this scenario).
Here is the relevant snippet:
var startTime = new Date();
//Add 90 minutes to the current date/time
var endTime = new Date();
endTime.setMinutes(startTime.getMinutes() + 90);
//Create a cvt_VOD record
var newVOD = {
'new_starttime': startTime,
'new_endtime': endTime
};
var createdVOD = CrmRestKit.Create('new_vod', newVOD)
...
I noticed that when I debug the create of the new_vod record in a pre-stage plugin, the "conversion" to UTC has already occurred incorrectly, so it leads me to believe that the Rest Call itself isn't right.
Can anyone shed any light on why this would be happening?
FYI, We're on CRM 2015 on-premise.
Try this working solution by Somesh:
https://community.dynamics.com/crm/b/dynamicscrmbestpractices/archive/2014/12/04/crm-timezone-issue-using-javascript
Related
I used recently a countdown in a product page. I used Jquery Countdown which is really great. I would like to synchronize the timer with server time because if the user changes time zone then hour(s) are added to the timer and thats the main problem.
In the site of the plugin (in the tab called time zones) there are some tips but dont really get it how to combine the scripts together, not much of a backend guy.
This is a part of my code: http://jsfiddle.net/zG9Ta/4/
Please check the bottom of my js cause I included the plugin inside the JS tab (and it had to be on top).
Any help how to combine my front end code with the server time?
Thanks in advance.
Main JS :
function call(){
$('.countdownclock').countdown({until: new Date(2014, 4-1,10,09,00), timezone:+3, format: 'HMS',padZeroes: true});
}
setTimeout(call,500);
The JavaScript runs client-side, so in order to have the JavaScript Date() show the client the time in Server time, server side (assuming a controller or action class), you will need to get the server's timezone's offset as an int and pass that to the page as a parameter. Then, in your JS Date, create a UTC date, then increment that Date by the offset.
In your controller/action:
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
int tzOffset = tz.getOffset(cal.getTimeInMillis());
In your JS:
var wallTimeDate = new Date();
var utcDate = new Date(wallTimeDate.getUTCFullYear(), wallTimeDate.getUTCMonth(), wallTimeDate.getUTCDate(), wallTimeDate.getUTCHours(), wallTimeDate.getUTCMinutes(), wallTimeDate.getUTCSeconds());
var serverDate = new Date(now_utc.getTime() + tzOffset);
You can see all of the methods for JS Date at W3C JS UTC
Once you have your Date, pass that to your JQuery function and use that instead of the one you have. This should get you what you need.
I'm trying to time out email messages based on user preferences. My morning calculations are working correctly but it's the evening emails that are never getting sent because dates aren't behaving as expected.
First, here's the code I'm using to grab the time and perform adjustments based on user location, etc.
var time = moment();
var machineTZ = time.zone();
var userTZ = 420;
var diffTZ = userTZ - machineTZ;
var oneHour = moment(time).add('minutes', 60);
var morningRun = moment().startOf('day');
morningRun.hour(7).minute(0);
morningRun.add('minutes', diffTZ);
var eveningRun = moment().startOf('day');
eveningRun.hour(19).minute(30);
eveningRun.add('minutes', diffTZ);
I'm checking every hour to see if it's time to schedule another email to go out. Right now this is hard-coded, but when I begin to add user preferences they'll be able to select their local time they'd like things to go out at.
I've been debugging my values to troubleshoot. Here's output from a job that ran early morning (from the server's perspective):
lastRun: 2013-10-12T00:06:55.088Z (this one is being run at 1 am)
morningRun: 2013-10-11T14:00:00.000Z
eveningRun: 2013-10-12T02:30:00.000Z
The run numbers are as I would expect them to be. In two hours time I want the evening email to go out (7:30pm my time = 2:30am the following day server-time).
Looking again an hour later we see:
lastRun: 2013-10-12T01:06:58.267Z (this one is at 2 am)
morningRun: 2013-10-12T14:00:00.000Z
eveningRun: 2013-10-13T02:30:00.000Z <---- what?
All of a sudden my calculation for my evening has flipped over the date line, even though it's still 10/12 (not 10/13 yet). Because of this my check to see if I should schedule the email fails since it now thinks I need to send the email in 24 hours, not 30 minutes.
Been battling with this weird inconsistency for a while, I thought I had figured out why it was doing it and adjusted my calculations using the time zone stuff above but that didn't do the trick :(. This certainly seems like some sort of weird bug as I would expect this to be happening:
//Today is 10/12
var eveningRun = moment().startOf('day'); //10/12/2013 - 00:00:00
eveningRun.hour(19).minute(30); //10/12/2013 - 19:30
eveningRun.add('minutes', diffTZ); //10/13/2013 - 2:30 am
This works until at some point it decides that "today" is actually 10/13 and sets the evening run is to take place on 10/14 instead. Help is greatly appreciated, or if this is a bug would love to know what I can do to work around this issue until it's resolved.
There's no need to calculate machineTZ, diffTZ or add any minutes. Just do this instead:
moment().zone(userTZ).startOf('day').hour(7).minute(0)
But do keep in mind that a value such as 420 is not a time zone, it's a time zone offset. It only tells you what the offset is for a particular point in time. Since you are applying it unilaterally, you may get incorrect results during daylight saving time.
Instead, you should try using the moment-timezone addon, and do something like this instead:
moment().tz("America/Los_Angeles").startOf('day').hour(7).minute(0)
See also the timezone tag wiki, in particular the sections titled "Time Zone != Offset" and "The IANA/Olson Time Zone Database".
I have a field in my database which states the timezone of the user. It is an integer field so anything > 0 is a + hour.
I need to display two separate times on the users screen
e.g.
Your time is: "just get the current computer time"
The other users time is: "get the current computer time then either add or subtract hours based on the database record"
I can do the database side of things I just need help with the Javascript element. For demonstration purposes I am happy for you to hard code the offset
The time must update automatically every 60 seconds to reflect the new minute and hour
Thanks
You can just set the hours on the date object in JavaScript. For example:
var date = new Date();
date.setHours((new Date()).getHours() - 5);
It will also try and be clever and make adjustments for you, as MDN says:
If a parameter you specify is outside of the expected range, setHours
attempts to update the date information in the Date object
accordingly. For example, if you use 100 for secondsValue, the minutes
will be incremented by 1 (min + 1), and 40 will be used for seconds.
You can use setInterval to perform the update. Here's a more complete example:
setInterval(function() {
var local = new Date();
var remote = new Date();
remote.setHours(local.getHours() - 5);
$('#time-local').html(local.toGMTString());
$('#time-ny').html(remote.toGMTString());
},1000);
When i write data in mongodb for the recording date i use new Date() in node.js and i return that date with ajax response. To calculate the time elapsed from the moment the data in mongodb i create a new date on the client side. Then i calculate the difference between the current date and the date of which came from the server.
Here is my code:
var now = new Date();
var post_date = new Date(date_from_server);
var elapsed = now - post_date/1000;
document.write(elapsed + " seconds elapsed");
But, unknown to me, the recording date is greater than the current date, and the difference between them for the first time has a value of -40 seconds!
Please help me to understand this point. I guess that's something I'm doing wrong. But what's wrong?
It seems that the time setup between the server and client is different. You may try to emit a getFinalTime event to server, where the second time is recorded and returned to client for calculation.
I have a database field called CreatedDate which is a timestamp field and holds the date and time. It currently holds the GMT time.
At the moment this field is displayed as it is on the web pages.
We want to amend this to show date/time as per browser local time zone
Can you please let us know how we can do this.
Thanks in advance
If you can represent the universal time as a count of milliseconds since the 1 Jan 1970 epoch at the server then you can have the page code construct Date instances at the client using the client time zone, but starting from that UTC reference. It's the normal behavior of the Javascript Date() constructor:
var clientDate = new Date(serverUTC);
Now exactly how you get that UTC value depends on your server language. In a JSP page it'd be pretty simple:
var clientDate = new Date(<%= whatever.getTheDate().getTime() %>);
or
var clientDate = new Date(${something.theDate.time});
Once you've got the date value as a client-side Javascript Date instance, you can just update the field(s) with the string. There are no built-in Date formatting tools, but the old standard date.js might help with its ".toString()" formatter.