1

Kindly help with displaying correct datetime stamp.

E.g. Time on my Laptop: 02-Oct-2021 11:14am (this is in India)

Time stored by Node and Displayed in Angular 2021-10-02T05:44:09.022Z, i.e. 06:30 behind my time. And this is being executed in dev mode on localhost, so it should display my laptop's time right?

Here is the code in the Node.js while saving one comment.

  const comment = {
    comment: remarks,
    userName: user.userName,
    commentBy: user.employeeName,
    commentedOn: new Date()
  }

  clientDoc.commentsArr.push(comment)
  await clientDoc.save();

and here is the code in Angular template.

      <div *ngIf="clientDoc" class="container">
        <span class="subTitle">Existing Comments:</span>
        <table style="width: 100%">
          <tr *ngFor="let e of clientDoc?.commentsArr, let i =index">
            <span style="font-style: italic; font-weight: 500;">{{i + 1}}. {{e.commentBy}} ( {{e.commentedOn}} ) : </span>
            <span *ngIf="account.role !== 'Client'">
              <a class="link small" (click)="onEditRemarks(e._id, e.comment)">Edit</a>
            </span>
            <br>
            <span style="font-weight: 350;">{{e.comment}}</span>
          </tr>
        </table>
      </div>

And here is the timestamp stored in MongoDb

2021-10-02T05:44:09.022+00:00

I need to sort this both for the dev mode, as well as when I deploy this code on VPS - which is also in India.

1 Answer 1

1

This happens because MongoDB always stores the timestamp in UTC.

Try below code:

var record = db.data.findOne();
var localNow = new Date( record.date.getTime() -  ( record.offset * 60000 ) );

For more reference, you can also check the MongoDB official Doc here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I saw the MongoDB doc. But does that mean every time the offset needs to be saved and the dates need to be reconstructed for using both in Node as well as Angular. In Node - in case any date based calculations are to be performed with respect to date received from Angular. And in Angular every time date needs to be reconstructed to be displayed to the user? Isn't there any other way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.