I tried this fiddle from this, this, this SO, and this.
I still can't make shuffle or random work on my end. My code has stopped here:
signupApp.filter('shuffle', function() {
var shuffledArr = [],
shuffledLength = 0;
return function(arr) {
if (!arr || !arr.length) { return; }
var o = arr.slice(0, arr.length);
if (shuffledLength == arr.length) return shuffledArr;
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
shuffledArr = o;
shuffledLength = o.length;
return arr.slice(o);
};
});
my ng-repeat
<ul ng-repeat="codes in response | shuffle">
<li ng-if="((codes.branch == formData.branches.alias) && (codes.taken == 0))">
<strong>{{codes.id}}</strong>
{{codes.code}}
</li>
</ul>
Results still do not randomize my outputs. Can someone help?
[UPDATE]: My code works in randomizing ng-repeat:
<span ng-repeat="codes in response">
<strong ng-if="((codes.branch == formData.branches.alias) && (codes.taken == 0))">
{{codes.code}}
</strong>
</span>
When limitTo:1 is added, it no longer displays anything. In my controller, the fisher-yates is run like this:
var shuffleArray = function(array) {
var m = array.length, t, i;
// While there remain elements to shuffle
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
http.get("server/fetch.php").success(function(response){
scope.response = response;
shuffleArray(scope.response);
}).error(function() {
scope.response = "error in fetching data";
});
I can't figure out what's wrong with this. When limitTo: is set to 6, it outputs a single random value from the list. I also tried to set it to 10, and it outputs two random values from the list. I'm confused. I just want to limit it to 1 and randomize it.
Related
This asignCards() function is supposed to give random cards from the cards array to each player. The cards are not supposed to be repeated but I get repeated cards.
// For each player in the array of players, each player receives two cards.
const asignCards = () => {
players.forEach((player) => {
let useCards = [];
let inUse = cards;
for (let i = 0; i < 1; i++) {
let random = Math.floor(Math.random() * 52);
let random2 = Math.floor(Math.random() * 52);
if (
useCards.indexOf(JSON.stringify(inUse[random])) === -1 ||
useCards.indexOf(JSON.stringify(inUse[random2])) === -1 ||
useCards.indexOf(JSON.stringify(inUse[random])) !==
useCards.indexOf(JSON.stringify(inUse[random2]))
) {
player.card1 = inUse[random];
player.card2 = inUse[random2];
useCards.push(JSON.stringify(player.card1));
useCards.push(JSON.stringify(player.card2));
}else {
i--;
}
}
console.log(player);
});
};
asignCards();
You should declare your useCards array outside of the loop, otherwise everytime it will be empty at each iteration. Put it that way, you can not have duplicate for a player, but the same card can be assigned to more players.
const asignCards = () => {
let useCards = [];
players.forEach((player) => {
let inUse = cards;
for (let i = 0; i < 1; i++) {
let random = Math.floor(Math.random() * 52);
let random2 = Math.floor(Math.random() * 52);
if (
useCards.indexOf(JSON.stringify(inUse[random])) === -1 ||
useCards.indexOf(JSON.stringify(inUse[random2])) === -1 ||
useCards.indexOf(JSON.stringify(inUse[random])) !==
useCards.indexOf(JSON.stringify(inUse[random2]))
) {
player.card1 = inUse[random];
player.card2 = inUse[random2];
useCards.push(JSON.stringify(player.card1));
useCards.push(JSON.stringify(player.card2));
}else {
i--;
}
}
console.log(player);
});
};
asignCards();
Bonus: you may want to rename your function to assignCards (from assign) with double s.
Update:
Here's a different approach. Every time you want to play you shuffle the deck first, then pick your amount of cards from the top of the deck with Array.pop(), so you're 100% sure there will be no duplicates.
let players = ["A", "B", "C", "D"];
let cards = [...Array(52).keys()];
const cardsPerPlayer = 2;
const asignCards = () => {
// Shuffle deck
var n = cards.length;
while (n--) {
var i = Math.floor(n * Math.random());
var tmp = cards[i];
cards[i] = cards[n];
cards[n] = tmp;
}
players.forEach((player, index) => {
let myCards = [];
for (c = 0; c < cardsPerPlayer; c++) {
myCards.push(cards.pop());
}
console.log(`${player} has the following cards: ${myCards}`);
});
};
asignCards();
I'm trying to change the grade of a response based on its answer.
Here's the code I'm using:
function myFunction() {
var form = FormApp.openById('formID123456');
// For a question with options: "1", "2", "3", and "4",
// award points for responses that correlate with their answers.
var formResponses = FormApp.getActiveForm().getResponses();
// Go through each form response
for (var i = 0; i < formResponses.length; i++) {
var response = formResponses[i];
var items = FormApp.getActiveForm().getItems();
// Assume it's the first item
var item = items[0];
var itemResponse = response.getGradableResponseForItem(item);
// Give 4 points for "4".
if (itemResponse != null && itemResponse.getResponse() == '4') {
var points = item.asScaleItem().getPoints();
itemResponse.setScore(points == 4);
}
// Give 3 points for "3".
else if (itemResponse != null && itemResponse.getResponse() == '3') {
var points = item.asScaleItem().getPoints();
itemResponse.setScore(points == 3);
}
// Give 2 points for "2".
else if (itemResponse != null && itemResponse.getResponse() == '2') {
var points = item.asScaleItem().getPoints();
itemResponse.setScore(points == 2);
}
// Give 1 points for "1".
else if (itemResponse != null && itemResponse.getResponse() == '1') {
var points = item.asScaleItem().getPoints();
itemResponse.setScore(points == 1);
// This saves the grade, but does not submit to Forms yet.
response.withItemGrade(itemResponse);
}
}
// Grades are actually submitted to Forms here.
FormApp.getActiveForm().submitGrades(formResponses);
}
This returns the error:
We're sorry, a server error occurred. Please wait a bit and try again. (line 23, file "Code")
It seemed like it was having issues changing the score of the response, but it didn't return a specific error, so I tried to isolate the part that changes the score.
Here, the script attempts only to change the score of the response.
function myFunction() {
var form = FormApp.openById('formID123456');
var formResponses = FormApp.getActiveForm().getResponses();
// Go through each form response
for (var i = 0; i < formResponses.length; i++) {
var response = formResponses[i];
var items = FormApp.getActiveForm().getItems();
// Assume it's the first item
var item = items[0];
var itemResponse = response.getGradableResponseForItem(item);
// Set Score to 3
var points = item.asScaleItem().getPoints();
itemResponse.setScore(points == 3);
}}
Again, it returned the same error, which confirms my suspicions. Why am I having this problem and how can I fix it? Any help would be much appreciated. Thanks!
As I mentioned in comments, your posted code erroneously uses a Boolean value in the call to ItemResponse#setScore, when the method expects to receive an Integer value.
Resolving the internal server error can be done by changing your entire if-elseif chain from this:
if (itemResponse != null && itemResponse.getResponse() == '4') {
var points = item.asScaleItem().getPoints();
itemResponse.setScore(points == 4); //<--- 'points == 4' evaluates to True or False
}
// Give 3 points for "3".
else if (...
to this:
// Skip changing the score if there was no answer or the answer is "falsey"
if (!itemResponse || !itemResponse.getResponse())
continue;
var answer = itemResponse.getResponse();
var newPoints = answer *1; // Convert "2" to 2, etc.
// Assumption: newPoints <= maximum possible points.
itemResponse.setScore(newPoints);
response.withItemGrade(itemResponse);
The below code is an example of how to set all graded items in all responses to a form to their maximum possible value.
function everyonePassesForTrying() {
var form = FormApp.getActiveForm();
var responses = form.getResponses();
responses.forEach(function (fr) {
fr.getGradableItemResponses().forEach(function (gr) {
if (gr.getResponse()) {
var maxPoints = getPointValue_(gr.getItem());
if (gr.getScore() !== maxPoints) {
// Re-grade the item's response.
gr.setScore(maxPoints);
// Update the form response with the new grade.
fr.withItemGrade(gr);
}
}
else { /* They didn't even try, so no change */ }
});
});
// Submit the altered scores.
form.submitGrades(responses);
}
var itemCache = {};
function getPointValue_(item) {
var id = item.getId();
// Use a pseudo-cache of the item's point values to avoid constantly determining what
// type it is, casting to that type, and getting the max points.
if(!itemCache[id]) {
// Haven't seen it yet, so cast and cache.
item = castToType_(item);
itemCache[id] = {maxPoints: item.getPoints() *1};
}
return itemCache[id].maxPoints;
}
function castToType_(item) {
// Cast the generic item to its type.
var CHECKBOX = FormApp.ItemType.CHECKBOX,
DATE = FormApp.ItemType.DATE,
DATETIME = FormApp.ItemType.DATETIME,
DURATION = FormApp.ItemType.DURATION,
LIST = FormApp.ItemType.LIST,
MULTICHOICE = FormApp.ItemType.MULTIPLE_CHOICE,
PARAGRAPH = FormApp.ItemType.PARAGRAPH_TEXT,
SCALE = FormApp.ItemType.SCALE,
TEXT = FormApp.ItemType.TEXT,
TIME = FormApp.ItemType.TIME;
switch (item.getType()) {
case CHECKBOX: item = item.asCheckboxItem();
break;
case DATE: item = item.asDateItem();
break;
case DATETIME: item = item.asDateTimeItem();
break;
case DURATION: item = item.asDurationItem();
break;
case LIST: item = item.asListItem();
break;
case MULTICHOICE: item = item.asMultipleChoiceItem();
break;
case PARAGRAPH: item = item.asParagraphTextItem();
break;
case SCALE: item = item.asScaleItem();
break;
case TEXT: item = item.asTextItem();
break;
case TIME: item = item.asTimeItem();
break;
default:
throw new Error("Unhandled gradable item type '" + item.getType() + "'");
break;
}
return item;
}
Update #2: Okay, I'm pretty sure my error in update #1 was because of indexing out of bounds over the array (I'm still not used to JS indexing at 0). But here is the new problem... if I write out the different combinations of the loop manually, setting the page index to 1 in moveItem() like so:
newForm.moveItem(itemsArray[0][0], 1);
newForm.moveItem(itemsArray[0][1], 1);
newForm.moveItem(itemsArray[0][2], 1);
newForm.moveItem(itemsArray[1][0], 1);
newForm.moveItem(itemsArray[1][1], 1);
newForm.moveItem(itemsArray[1][2], 1);
newForm.moveItem(itemsArray[2][0], 1);
...
...I don't get any errors but the items end up on different pages! What is going on?
Update #1:: Using Sandy Good's answer as well as a script I found at this WordPress blog, I have managed to get closer to what I needed. I believe Sandy Good misinterpreted what I wanted to do because I wasn't specific enough in my question.
I would like to:
Get all items from a page (section header, images, question etc)
Put them into an array
Do this for all pages, adding these arrays to an array (i.e: [[all items from page 1][all items from page 2][all items from page 3]...])
Shuffle the elements of this array
Repopulate a new form with each element of this array. In this way, page order will be randomized.
My JavaScript skills are poor (this is the first time I've used it). There is a step that produces null entries and I don't know why... I had to remove them manually. I am not able to complete step 5 as I get the following error:
Cannot convert Item,Item,Item to (class).
"Item,Item,Item" is the array element containing all the items from a particular page. So it seems that I can't add three items to a page at a time? Or is something else going on here?
Here is my code:
function shuffleForms() {
var itemsArray,shuffleQuestionsInNewForm,fncGetQuestionID,
newFormFile,newForm,newID,shuffle, sections;
// Copy template form by ID, set a new name
newFormFile = DriveApp.getFileById('1prfcl-RhaD4gn0b2oP4sbcKaRcZT5XoCAQCbLm1PR7I')
.makeCopy();
newFormFile.setName('AAAAA_Shuffled_Form');
// Get ID of new form and open it
newID = newFormFile.getId();
newForm = FormApp.openById(newID);
// Initialize array to put IDs in
itemsArray = [];
function getPageItems(thisPageNum) {
Logger.log("Getting items for page number: " + thisPageNum );
var thisPageItems = []; // Used for result
var thisPageBreakIndex = getPageItem(thisPageNum).getIndex();
Logger.log( "This is index num : " + thisPageBreakIndex );
// Get all items from page
var allItems = newForm.getItems();
thisPageItems.push(allItems[thisPageBreakIndex]);
Logger.log( "Added pagebreak item: " + allItems[thisPageBreakIndex].getIndex() );
for( var i = thisPageBreakIndex+1; ( i < allItems.length ) && ( allItems[i].getType() != FormApp.ItemType.PAGE_BREAK ); ++i ) {
thisPageItems.push(allItems[i]);
Logger.log( "Added non-pagebreak item: " + allItems[i].getIndex() );
}
return thisPageItems;
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
Logger.log('shuffle ran')
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function shuffleAndMove() {
// Get page items for all pages into an array
for(i = 2; i <= 5; i++) {
itemsArray[i] = getPageItems(i);
}
// Removes null values from array
itemsArray = itemsArray.filter(function(x){return x});
// Shuffle page items
itemsArray = shuffle(itemsArray);
// Move page items to the new form
for(i = 2; i <= 5; ++i) {
newForm.moveItem(itemsArray[i], i);
}
}
shuffleAndMove();
}
Original post: I have used Google forms to create a questionnaire. For my purposes, each question needs to be on a separate page but I need the pages to be randomized. A quick Google search shows this feature has not been added yet.
I see that the Form class in the Google apps script has a number of methods that alter/give access to various properties of Google Forms. Since I do not know Javascript and am not too familiar with Google apps/API I would like to know if what I am trying to do is even possible before diving in and figuring it all out.
If it is possible, I would appreciate any insight on what methods would be relevant for this task just to give me some direction to get started.
Based on comments from Sandy Good and two SE questions found here and here, this is the code I have so far:
// Script to shuffle question in a Google Form when the questions are in separate sections
function shuffleFormSections() {
getQuestionID();
createNewShuffledForm();
}
// Get question IDs
function getQuestionID() {
var form = FormApp.getActiveForm();
var items = form.getItems();
arrayID = [];
for (var i in items) {
arrayID[i] = items[i].getId();
}
// Logger.log(arrayID);
return(arrayID);
}
// Shuffle function
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}
// Shuffle IDs and create new form with new question order
function createNewShuffledForm() {
shuffle(arrayID);
// Logger.log(arrayID);
var newForm = FormApp.create('Shuffled Form');
for (var i in arrayID) {
arrayID[i].getItemsbyId();
}
}
Try this. There's a few "constants" to be set at the top of the function, check the comments. Form file copying and opening borrowed from Sandy Good's answer, thanks!
// This is the function to run, all the others here are helper functions
// You'll need to set your source file id and your destination file name in the
// constants at the top of this function here.
// It appears that the "Title" page does not count as a page, so you don't need
// to include it in the PAGES_AT_BEGINNING_TO_NOT_SHUFFLE count.
function shuffleFormPages() {
// UPDATE THESE CONSTANTS AS NEEDED
var PAGES_AT_BEGINNING_TO_NOT_SHUFFLE = 2; // preserve X intro pages; shuffle everything after page X
var SOURCE_FILE_ID = 'YOUR_SOURCE_FILE_ID_HERE';
var DESTINATION_FILE_NAME = 'YOUR_DESTINATION_FILE_NAME_HERE';
// Copy template form by ID, set a new name
var newFormFile = DriveApp.getFileById(SOURCE_FILE_ID).makeCopy();
newFormFile.setName(DESTINATION_FILE_NAME);
// Open the duplicated form file as a form
var newForm = FormApp.openById(newFormFile.getId());
var pages = extractPages(newForm);
shuffleEndOfPages(pages, PAGES_AT_BEGINNING_TO_NOT_SHUFFLE);
var shuffledFormItems = flatten(pages);
setFormItems(newForm, shuffledFormItems);
}
// Builds an array of "page" arrays. Each page array starts with a page break
// and continues until the next page break.
function extractPages(form) {
var formItems = form.getItems();
var currentPage = [];
var allPages = [];
formItems.forEach(function(item) {
if (item.getType() == FormApp.ItemType.PAGE_BREAK && currentPage.length > 0) {
// found a page break (and it isn't the first one)
allPages.push(currentPage); // push what we've built for this page onto the output array
currentPage = [item]; // reset the current page to just this most recent item
} else {
currentPage.push(item);
}
});
// We've got the last page dangling, so add it
allPages.push(currentPage);
return allPages;
};
// startIndex is the array index to start shuffling from. E.g. to start
// shuffling on page 5, startIndex should be 4. startIndex could also be thought
// of as the number of pages to keep unshuffled.
// This function has no return value, it just mutates pages
function shuffleEndOfPages(pages, startIndex) {
var currentIndex = pages.length;
// While there remain elements to shuffle...
while (currentIndex > startIndex) {
// Pick an element between startIndex and currentIndex (inclusive)
var randomIndex = Math.floor(Math.random() * (currentIndex - startIndex)) + startIndex;
currentIndex -= 1;
// And swap it with the current element.
var temporaryValue = pages[currentIndex];
pages[currentIndex] = pages[randomIndex];
pages[randomIndex] = temporaryValue;
}
};
// Sourced from elsewhere on SO:
// https://stackoverflow.com/a/15030117/4280232
function flatten(array) {
return array.reduce(
function (flattenedArray, toFlatten) {
return flattenedArray.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
},
[]
);
};
// No safety checks around items being the same as the form length or whatever.
// This mutates form.
function setFormItems(form, items) {
items.forEach(function(item, index) {
form.moveItem(item, index);
});
};
I tested this code. It created a new Form, and then shuffled the questions in the new Form. It excludes page breaks, images and section headers. You need to provide a source file ID for the original template Form. This function has 3 inner sub-functions. The inner functions are at the top, and they are called at the bottom of the outer function. The arrayOfIDs variable does not need to be returned or passed to another function because it is available in the outer scope.
function shuffleFormSections() {
var arrayOfIDs,shuffleQuestionsInNewForm,fncGetQuestionID,
newFormFile,newForm,newID,items,shuffle;
newFormFile = DriveApp.getFileById('Put the source file ID here')
.makeCopy();
newFormFile.setName('AAAAA_Shuffled_Form');
newID = newFormFile.getId();
newForm = FormApp.openById(newID);
arrayOfIDs = [];
fncGetQuestionID = function() {
var i,L,thisID,thisItem,thisType;
items = newForm.getItems();
L = items.length;
for (i=0;i<L;i++) {
thisItem = items[i];
thisType = thisItem.getType();
if (thisType === FormApp.ItemType.PAGE_BREAK ||
thisType === FormApp.ItemType.SECTION_HEADER ||
thisType === FormApp.ItemType.IMAGE) {
continue;
}
thisID = thisItem.getId();
arrayOfIDs.push(thisID);
}
Logger.log('arrayOfIDs: ' + arrayOfIDs);
//the array arrayOfIDs does not need to be returned since it is available
//in the outermost scope
}// End of fncGetQuestionID function
shuffle = function() {// Shuffle function
var j, x, i;
Logger.log('shuffle ran')
for (i = arrayOfIDs.length; i; i--) {
j = Math.floor(Math.random() * i);
Logger.log('j: ' + j)
x = arrayOfIDs[i - 1];
Logger.log('x: ' + x)
arrayOfIDs[i - 1] = arrayOfIDs[j];
arrayOfIDs[j] = x;
}
Logger.log('arrayOfIDs: ' + arrayOfIDs)
}
shuffleQuestionsInNewForm = function() {
var i,L,thisID,thisItem,thisQuestion,questionType;
L = arrayOfIDs.length;
for (i=0;i<L;i++) {
thisID = arrayOfIDs[i];
Logger.log('thisID: ' + thisID)
thisItem = newForm.getItemById(thisID);
newForm.moveItem(thisItem, i)
}
}
fncGetQuestionID();//Get all the question ID's and put them into an array
shuffle();
shuffleQuestionsInNewForm();
}
I want my questions to be shown in random order. I did it, but it doesn't work properly, sometimes it doesn't loop through all objects and stops and etc.
$scope.move = function (direction) {
var position = $scope.allData.indexOf($scope.currentQ);
$scope.currentQ = $scope.allData[position + direction];
};
$http.get("js/json/questions.json").then(function(response){
$scope.allData = response.data;
$scope.currentQ = $scope.allData[Math.floor(Math.random() * response.data.length)];
});
$scope.answerClick = function(index){
$scope.clicks++;
if(index === $scope.currentQ.answer){
$scope.score++;
$scope.rightwrong = "Right";
$('.right-wrong').css("color", "green");
}
else{
$scope.rightwrong = "Wrong";
$('.right-wrong').css("color", "red");
}
if($scope.clicks === 4){
resultService.score = $scope.score;
$location.path('/finish');
}
$scope.move(+1);
};
Here is the whole app: http://plnkr.co/edit/wTQHOz
In the move method you can get outside the range of the array. Make it wrap around if you move before the first or after the last question:
$scope.currentQ = $scope.allData[(position + direction + $scope.allData.length) % $scope.allData.length];
When running our AngularJS app in IE11 everything looks great in the debugger, but when our app encodes the data as JSON to save to our database, we get bad results.
Our app obtains a record from our database, then some manipulation is done and then the data is saved back to the server from another model.
Here is the data I got back from the server in the setAttendanceGetSInfo() function below:
{"data":{"Start":"2014-10-16T19:36:00Z","End":"2014-10-16T19:37:00Z"},
This is the code used to "convert the data" to 3 properties in our model:
var setAttendanceGetSInfo = function (CourseId, PID) {
return setAttendanceInfo(CourseId, PID)
.then(function (result) {
return $q.all([
$http.get("../api/Axtra/getSInfo/" + model.event.Id),
$http.get("../api/Axtra/GetStartAndEndDateTime/" + aRow.Rid)
]);
}).then(function (result) {
var r = result.data;
var e = Date.fromISO(r.Start);
var f = Date.fromISO(r.End);
angular.extend(model.event, {
examDate: new Date(e).toLocaleDateString(),
examStartTime: (new Date(e)).toLocaleTimeString(),
examEndTime: (new Date(f)).toLocaleTimeString()
});
return result.sInfo;
});
};
fromISO is defined as:
(function(){
var D= new Date('2011-06-02T09:34:29+02:00');
if(!D || +D!== 1307000069000){
Date.fromISO= function(s){
var day, tz,
rx=/^(\d{4}\-\d\d\-\d\d([tT ][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/,
p= rx.exec(s) || [];
if(p[1]){
day= p[1].split(/\D/);
for(var i= 0, L= day.length; i<L; i++){
day[i]= parseInt(day[i], 10) || 0;
};
day[1]-= 1;
day= new Date(Date.UTC.apply(Date, day));
if(!day.getDate()) return NaN;
if(p[5]){
tz= (parseInt(p[5], 10)*60);
if(p[6]) tz+= parseInt(p[6], 10);
if(p[4]== '+') tz*= -1;
if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
}
return day;
}
return NaN;
}
}
else{
Date.fromISO= function(s){
return new Date(s);
}
}
})()
Take a look at the screenshot of the event model data:
But, if I eval the event model using JSON.stringify(model.event), I get this:
{\"examDate\":\"?10?/?16?/?2014\",\"examStartTime\":\"?2?:?44?:?00? ?PM\",\"examEndTime\":\"?2?:?44?:?00? ?PM\"}
And this is the JSON encoded data that actually got stored on the DB:
"examDate":"¿10¿/¿16¿/¿2014","examStartTime":"¿2¿:¿36¿:¿00¿ ¿PM","examEndTime":"¿2¿:¿37¿:¿00¿ ¿PM"
What is wrong here and how can I fix this? It works exactly as designed in Chrome and Firefox. I have not yet tested on Safari or earlier versions of IE.
The toJSON for the date class isn't defined perfectly the same for all browsers.
(You can see a related question here: Discrepancy in JSON.stringify of date values in different browsers
I would suspect that you have a custom toJSON added to the Date prototype since your date string doesn't match the standard and that is likely where your issue is. Alternatively, you can use the Date toJSON recommended in the above post to solve your issues.
First, I modified the fromISO prototype to this:
(function () {
var D = new Date('2011-06-02T09:34:29+02:00');
if (!D || +D !== 1307000069000) {
Date.fromISO = function (s) {
var D, M = [], hm, min = 0, d2,
Rx = /([\d:]+)(\.\d+)?(Z|(([+\-])(\d\d):(\d\d))?)?$/;
D = s.substring(0, 10).split('-');
if (s.length > 11) {
M = s.substring(11).match(Rx) || [];
if (M[1]) D = D.concat(M[1].split(':'));
if (M[2]) D.push(Math.round(M[2] * 1000));// msec
}
for (var i = 0, L = D.length; i < L; i++) {
D[i] = parseInt(D[i], 10);
}
D[1] -= 1;
while (D.length < 6) D.push(0);
if (M[4]) {
min = parseInt(M[6]) * 60 + parseInt(M[7], 10);// timezone not UTC
if (M[5] == '+') min *= -1;
}
try {
d2 = Date.fromUTCArray(D);
if (min) d2.setUTCMinutes(d2.getUTCMinutes() + min);
}
catch (er) {
// bad input
}
return d2;
}
}
else {
Date.fromISO = function (s) {
return new Date(s);
}
}
Date.fromUTCArray = function (A) {
var D = new Date;
while (A.length < 7) A.push(0);
var T = A.splice(3, A.length);
D.setUTCFullYear.apply(D, A);
D.setUTCHours.apply(D, T);
return D;
}
Date.toJSON = function (key) {
return isFinite(this.valueOf()) ?
this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z' : null;
};
})()
Then I added moment.js and formatted the dates when they get stored:
var SaveAffRow = function () {
// make sure dates on coursedate and event are correct.
var cd = model.a.courseDate;
var ed = model.event.examDate;
var est = model.event.examStartTime;
var eet = model.event.examEndTime;
model.a.courseDate = moment(cd).format("MM/DD/YYYY");
model.event.examDate = moment(ed).format("MM/DD/YYYY");
model.event.examStartTime = moment(est).format("MM/DD/YYYY hh:mm A");
model.event.examEndTime = moment(eet).format("MM/DD/YYYY hh:mm A");
affRow.DocumentsJson = angular.toJson({a: model.a, event: model.event});
var aff = {};
if (affRow.Id != 0)
aff = affRow.$update({ Id: affRow.Id });
else
aff = affRow.$save({ Id: affRow.Id });
return aff;
};
and when they get read (just in case they are messed up already):
var setAttendanceGetSInfo = function (CourseId, PID) {
return setAttendanceInfo(CourseId, PID)
.then(function (result) {
return $q.all([
$http.get("../api/Axtra/getSInfo/" + model.event.Id),
$http.get("../api/Axtra/GetStartAndEndDateTime/" + aRow.Rid)
]);
}).then(function (result) {
var r = result.data;
var e = Date.fromISO(r.Start);
var f = Date.fromISO(r.End);
angular.extend(model.event, {
examDate: moment(e).format("MM/DD/YYYY"),
examStartTime: moment(e).format("MM/DD/YYYY hh:mm A"),
examEndTime: moment(f).format("MM/DD/YYYY hh:mm A")
});
return result.sInfo;
});
};