Question: I'm using moment.js to calculate the difference between two dates in Qualtrics. The code below has worked really well so far. The dates use a YY-mm-dd format, and at the end, I get the difference in days.

```
const duedate= Qualtrics.SurveyEngine.getEmbeddedData('duedate');
const daystodue = moment(duedate).diff(moment(new Date()), 'days', false);
Qualtrics.SurveyEngine.setEmbeddedData('daystill', daystodue );

```

The problem is when I change the format for the embedded data `'duedate'` from YY-mm-dd to ISO 8601 (e.g., 2021-10-19T20:30:48Z) this stops working.

I know I'd probably need to change the format for `new Date()` to ISO as well, or re-format `'duedate'` back to YY-mm-dd, but I'm struggling to figure this out. This is what I tried:

```
const duedate= Qualtrics.SurveyEngine.getEmbeddedData('duedate');
const daystodue = moment(duedate).diff(moment(new Date().format("YYYY-MM- 
DDTHH:mm:ssZ")), 'days', false);
Qualtrics.SurveyEngine.setEmbeddedData('daystill', daystodue );

```

I was basing myself on [this article](https://stackoverflow.com/questions/26500694/convert-date-from-one-format-to-another-format-in-javascript), but I'm a beginner in JavaScript and couldn't figure out how to make it work.

Any help at all is much appreciated!

Answer: 
Your function populates `x` just fine if the input file is properly formatted♰ - but there's one big flaw. You read, `while (input >> x)`, before checking if you *should* read. If you've already read all values in the file at that point, it'll set the `failbit` on the input stream, so if you do this to check:

```
if(puzzle >> x) {
    std::cout << "success\n";
} else {
    std::cout << "failure\n";
}

```

It will print, `failure`, even though all values were read - just because you tried to read one value too much.

I suggest simplifying it to only read exactly as many values as there are (9 x 9), and also, make it read from *any* `istream`, not only `ifstream`s.

The `friend` declaration becomes:

```
friend std::istream& operator>>(std::istream &input, Sudoku &s);

```

And the implementation:

```
std::istream& operator>>(std::istream &input, Sudoku &s) {
    for(int i = 0; i < 9; ++i) {
        for(int j = 0; j < 9; ++j) {
            // There's no need to use setValue since you made it a friend
            input >> s.grid[i][j];
        }
    }
    return input;
}

```

♰Inspect your input file. Does it have whitespaces between the numbers? If it doesn't - make sure it does or else your `operator>>` will not work.
Below is a complete, working example. You'll notice a few things.

1. I never use friend classes, and there was no reason to do so.
2. I made an operator>> function so I could write it back out.
3. My operator<< expects an istream rather than the more specific ifstream. The method shouldn't care that it's an ifstream, so making it more generic is better.
4. And mine is simpler than yours.

It compiles (at least under C++17) and runs properly. I'm not entirely sure why yours doesn't work.

```
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;
using std::istream;

class Sudoku
{
public:
    Sudoku() {};
    ~Sudoku(){}
    int getValue(int row, int col){return grid[row][col];}
    void setValue(int row,int col,int num) {grid[row][col]=num;}

private:
    int grid[9][9];
};

std::istream & operator>> (std::istream &input, Sudoku &s)
{
    int row = 0;
    int col = 0;
    while (row < 9) {
        int cellValue;
        if (!(input >> cellValue)) {
            std::cout << "Out of input." << endl;
            return input;
        }
        s.setValue(row, col, cellValue);
        if (++col >= 9) {
            ++row;
            col = 0;
        }
    }
    return input;
}

std::ostream & operator<< (std::ostream &oStr, Sudoku &s) {
    for (int row = 0; row < 9; ++row) {
        for (int col = 0; col < 9; ++col) {
            if (col % 3 == 0) {
                oStr << " ";
            }
            oStr << s.getValue(row, col);
        }
        oStr << endl;
    }
    return oStr;
}

int main(int, char **) {
    std::ifstream input("sudoku.txt");
    Sudoku sudoku;

    input >> sudoku;
    std::cout << sudoku << std::endl;
}

```