Can't store date as start of the day in mongoose - node.js

I am storing a date in mongodb using mongose with the help of moment js.
I am creating the date object from a date string which is in the format MM/DD/YYYY.
below is how i assign the date
const startDate = momentTz(
data.startDate,
"MM/DD/YYYY",
"Asia/Kolkata"
).startOf("day");
but ever time I assign this date object to the mongoose model while creating a document it is storing as 2022-11-30T18:30:00.000+00:00 ie, the time is being automatically set to 18:30.
how can i set this to the start of the day.

Use this one:
const startDate = momentTz(
data.startDate,
"MM/DD/YYYY",
"Asia/Kolkata"
).startOf("day").toDate();
MongoDB stores Date values as UTC times - always and only. It is the client responsibility to display the date/time in local time zone and format.

MongoDB stores dates as BSON type 9, which stores the number of milliseconds since 1970-01-01 00:00 UTC.
18:30 UTC is midnight in Kolkata, so that date/time is correctly showing the start of the day 2022-11-31 IST.
It is up to the client program to convert from UTC to a different timezone if that is desired.

Related

Why is date string changed to day before?

I have a Postgres database. In a table I have a column of type „date“.
I try to insert into this db with node express
Let myDate = "2022-03-03";
DB.query("INSERT INTO mytable (mydate) VALUES ($1)", [myDate]);
But the date in the database is then "2022-03-02". Looks like node is doing some kind of date to datetime conversion and then depending on the timezone reconvert it (because my timezone is 1h behind UTC)
So who can explain what is going on and how to handle this correct?

ADF: How to Convert a datetime column (AM/PM) to UTC format?

I have a column where timestamp is 5/23/2022 8:45:34 PM. I want to create a new column with same data as old column but in UTC format 'yyyy-MM-dd HH:mm:ss' and this new datetime format is 7 hours behind UTC (UTC-7)
I tried doing in azure data factory derived column using toTimestamp before it converts to UTC but it always fail.
toTimestamp(column_name,'yyyy-MM-dd HH:mm:SS')
but it did not work and the result always NULL.
Can anyone help this data conversion to UTC ?
The reason you are getting null values for the newly added columns is because the format you specified in the toTimestamp() function is incorrect. The following is the sample data that I used to reproduce this issue. The date column here is of type String.
While using Derived column in the dataflow to create a new date column of timestamp type, write toTimestamp(date, 'MM/dd/yyyy hh:mm:ss a', 'UTC') expression as the value for this new column. Here, date is the column you want to convert to new date column and MM/dd/yyyy hh:mm:ss a is the format of the values in date column (a represents the AM/PM). You can also pass time zone value like UTC, GMT etc. which is optional.
The following is what the result looks like, with a new column of timestamp type. You can use this resulting data to perform further conversions in dataflow.

Date Format in pg-node

I have tried to insert date into the postgresql in 'dd.mm.yyyy' format.
First inserted the string type, then the date type
moment(new Date()).format('DD.MM.YYYY') but in database format always like 'yyyy-mm-dd'
What can I do?
Plesase, help

Change date format in express using mongoose

I stored date in database but whenever i am fetching then its format change
getting date - 2018-07-31T06:48:01.649Z
stored date in db - 2018-07-31 12:18:01.649
here is my code
var timee = result.created;
By default, dates are stored in UTC in MongoDB so you are seeing the difference in the time zone of stored and retrieved dates.
new Date() returns the current date as a Date object. The mongo shell wraps the Date object with the ISODate helper. The ISODate is in UTC.
You can read the MongoDB specification here.
There are several approaches that you can apply. Either convert the date to UTC timezone before saving it or convert into locale time zone after loading the date.
new Date(ISODate().toString() + 'UTC')
ISODate("2018-08-01T11:39:39Z")
and to replace the T and Z with an empty space using the javascript.
dateString.replace("T", " ").replace("Z", " ");

save date from date picker into core data

i work with swift 3 for macOS and would like to save my date picker value in core data.
I have an attribute (type date) in my entity.
I tried this code for save my date picker value into core date:
let entity = NSEntityDescription.entity(forEntityName: "Person", in: context)
let newRecord = NSManagedObject(entity: entity!, insertInto: context) as! Person
newRecord.bday = myDatePicker.dateValue as NSDate
This value will be save in CoreData:
517935600
is this correct? oO
Probably. It depends on what date you had selected in the date picker. NSDate and Date store the date as the number of seconds since a reference date, which is the start of January 1, 2000 UTC. It looks like the UTC value from your date picker was May 31 2017 at 9:00 UTC.

Resources