When formatting date object - does it matter that the T and the 000Z will be removed when storing to db?

juliascoding

Sorry if its a very basic question but I dont understand the following:

When I format the Date object (no matter what library I used), I get a string.

from this: 2022-11-28T16:55:44.000Z (new Date object)
I get this: 2022-11-28 16:55:44 (or other formats obviously depending how I format it)

Even if I turn it back into an object it, the T and 000Z will never be there anymore. Do I just ignore that (seems like it as any library or date methods are ignoring the T and the string ending when formatting) or do I add it 'back' Isnt it a problem if dates stored in my db are different (for later queries etc.)?

Wernfried Domscheit

I see it even stronger: You should never store dates as strings, it's a design flaw. Store always proper Date objects. Here on SO you can find hundreds of questions, where people have problems, because they stored date values as (localized) strings. It is not limited to MongoDB, it applies to any database.

Date objects in MongoDB are UTC times - always and only! Usually the client application is responsible to display the date/time in local time zone and local format.

What do you mean by "turn it back", i.e. how do you do it?

You should not rely on new Date(<string>) without time zone. Some browsers/environments may apply UTC time, others may use current local time zone, see Differences in assumed time zone

Have a look at 3rd party date libraries, e.g. moment.js, Luxon, or Day.js. Usually they provide better control how to parse strings and time zones.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I get rid of `.000Z` when using `Date.prototype.toISOString`?

When to choose type `{}` over `object` or does it matter?

Why Does it Matter if An Object is Nil When Destroyed?

Hibernate does not delete a DB record when an entity is removed from a collection

When does operator associativity matter?

In R - When Does Spacing Matter?

Format date string with .000Z into NSDate

Mongodb Node Driver - does it really matter when I call db.close() or am I getting lucky?

When does order of alternation matter in antlr?

When does the order of multiplication matter in python?

Does the timezone matter when parsing a Timestamp?

Does the order matter when declaring classes in Java?

Does it matter when we use the preventDefault method?

Does it matter to filter results when doing aggregation?

Does the Order Matter When Checking if a String isn't Null and also isn't Empty?

error: Cannot format given Object as a Date, when formatting date to new format (Android)

When using function_score in Elasticsearch, does it matter if filters are inside or out the function_score object?

Library Project - Updating the object when book is removed

How to update an HTML object when attribute is removed

sequelizejs saving an object when row was removed

Why does the break point not disappear when removed

Why does Blazor not re-render when object is removed from list?

What does %-8.8s mean when formatting? And can you explain this date formatting %1$tY%1$tm%1$td?

MomentJS changes date value when formatting?

Moment: add +1 Month when formatting the Date

angular-datatables not sorting when formatting date

Formatting Date in PHP when value is returned as a string

Include List of Class When Storing Object in Firebase

SortedSet - custom order when storing a class object