Date format of MongoDB (inserting using Mongoose) - node.js

This is a 2 question post:
When I enter a date for a document into MongoDB through an html form, I'll enter it using this format: YYYY-MM-DD
For example: 2014-02-13
However, in MongoDB, it inputs it in the ISOdate format, and while the YYYY-MM-DD will be correct, for whatever reason it will insert one of 3 different hour time stamps:
ISODate("2016-02-02T00:00:00.000Z")
ISODate("2015-02-16T05:00:00.000Z")
ISODate("2016-08-02T04:00:00.000Z")
I never specify the hour when entering dates, but for whatever reason it will always insert into MongoDB an hour of 0, 4, or 5.
I have no idea why it's doing this.
Alternatively, is there a way to remove the 'time' part of the date, seeing as I don't need it? For 'date' all I need is YYYY-MM-DD.

To return only the date as a date object you can try this:
var myDate = new Date("2016-05-18T16:00:00Z");

Related

In Excel how do I format cells correctly to work with the following date/time format?

Could someone please tell me what cell format I should use to format the following so that they are recognised in a date time format I can then use to sort from oldest date to newest date please?
02-DEC-21 21.32.01.666000000
02-DEC-21 22.16.50.588000000
03-DEC-21 00.31.06.414000000
03-DEC-21 03.50.11.644000000
03-OCT-21 18.04.41.267000000
04-DEC-21 05.39.27.832000000
I’ve tried using dd-mmm-yy hh.mm.ss.000 (can’t enter more than three 0’s in milliseconds) and also without the .000 at the end but the cells don’t get recognised as dates when I try and sort them
Thanks
You can use two formulas to retrieve the date part and the time part.
I named the range with the dates DateColumn
To retrieve the date part: =DATEVALUE(SUBSTITUTE(LEFT(DateColumn,9),".",":"))
To retrieve the time part: =TIMEVALUE(SUBSTITUTE(MID(DateColumn,11,8),".",":"))
To retrieve the whole date incl. time:
=DATEVALUE(SUBSTITUTE(LEFT(DateColumn,9),".",":")) + TIMEVALUE(SUBSTITUTE(MID(DateColumn,11,8),".",":"))
These formulas return a number, e.g. 44532 for 02-DEC-21 or 0,92835648 for 22.16.50 or 44532,92835648 for the whole date with time. You have to format them accordingly.
But be aware: this will only work on an english system.
E.g. on a German system only Dez or Okt would get recognized.

Power Automate - Compare StartDate to current date?

We are looking to compare the calendar start date with the current date in flow. Currently we are pulling the start date through a filter query like this:
formatDateTime('EventDate','M-d-yyy hh:mm')
and comparing to this:
formatDateTime(addDays(utcNow(),3),'M-d-yyyy hh:mm')
I am getting this error currently: "Unable to process template language expressions in action 'Get_items' inputs at line '1' and column '17987': 'In function 'formatDateTime', the value provided for date time string 'EventDate' was not valid. The datetime string must match ISO 8601 format."
Any help on this would be greatly appreciated!
You could Initialize a variable like the following below and then do a IF condition.
#{add(div(sub(ticks(utcNow()),ticks('1900-01-01')),864000000000),2)}
I test with the same formula to convert event start date and it works fine in my end. The if condition returns correct result on different conditions.
Here is the logic of my demo flow, please have a check:
I use get item to get the start date of a certain event in the calendar list and convert the start time dynamic content to the format I need. Almost same with yours.
According to the error message, it seems the format of the event date column is not in the supported format. Would you like to provide the settings of your event date column? I test with the default start time column and a custom date & time column of default settings. Both of them works fine with the flow above.
Update:
The two formulas used are displayed below:
formatDateTime(outputs('Get_item')?['body/Event_x0020_Date'],'M-d-yyy hh:mm')
formatDateTime(addDays(utcNow(),3),'M-d-yyyy hh:mm')
I just directly use the outputs of the data compose action in the if condition
What worked for me was to build a sequence of numbers with the dates being ordered by day, month and year this way, when converting to integer, it's easy to compare
int(formatDateTime(utcNow()),'yyyyMMdd'))
if you need add day for exemple, you can use
int(formatDateTime(addDays(utcNow(),variables('extraDays')),'yyyyMMdd'))
and the other date like this
int(formatDateTime(variables('FinalDate'),'yyyyMMdd'))
Based on your example, you can put hours and minutes without any problems, just remove the special characters and spaces
but by the definition of the error you are having, I believe it is because of the way you are writing the variable

New Date().toISOString() issues

This is a strange issue. In my mongodb, I have ISODate something like this:
ISODate("1937-08-03T00:00:00.000Z")
in my find, I am doing
var isoString = new Date(inputDate).toISOString();
db.collection.find({dob: new Date(isoString)})
'inputDate' cab be yyyy-MM-dd or MM/dd/yyyy
when I convert string that is user inputted '1937-08-03' using Date().toISOString(), so I get
1937-08-03T00:00:00.000Z
but when my date format changes to 08/03/1937, using Date().toISOString(), I see ISO string as
1937-08-03T04:00:00.000Z
if you notice, I see 4 hours added when my date format changed to MM/dd/yyyy from yyyy-MM-dd. so obviously with extra 4 hours added, I am not able to pull the record from the db (no match)
How do I work this?
I am using Node and mongo. Thanks for any help.
OK here is how I handled the work around. I know my input date formats are either MM/dd/yyyy or MM-dd-yyyy so I split the string based on / OR - using the regex, and then reformatted back to yyyy-mm-dd format which works fine. If anyone has better solution, please post it here.
var dtArray = inputDate.split(/-|\//);
var dtStr = dtArray[2]+'-'+dtArray[0]+'-'+dtArray[1];

Twig's date filter always return current date with valid timestamp

Im having an issue with Twig where any timestamp I pass to the date filter I always get the current date, example:
{{ 1433894400|date }}
This should return the June, 10. But instead it returns the current date.
I've tried passing a few different date formats but as expected it makes no change to the output, other than the date format itself that is.
Have anyone had this issue before?
Thank you.
By the twig docs:
The date filter accepts strings (it must be in a format supported by the strtotime function), DateTime instances, or DateInterval instances. For instance, to display the current date, filter the word "now".
Therefore, use date() to convert your Unix timestamp to a string.

How can I store date prior to B.C in MongoDB as ISODate?

In MongoDB I've stored dates prior to B.C has String type. How can I stored in ISODate() type ?
I've tried this :
db.test.insert({"date" : new ISODate("-63-09-23") })
but I get an error :
uncaught exception: invalid ISO date
Thanks for your help !
The MongoDB IsoDate() is just a wrapper around the normal javascript Date, which can take dates to 100 million days before 1970, so something like setFullYear() is probably what you need - like this: Can you create dates that are lower than 271800 BC? Like dinosaur time?
As Nik pointed out MongoDB dates are just javascript Dates. So you need to transform an ISODate back and forth to the underlying javascript Date. If you don't need to do queries based on the date you can also optionally store the date as a string instead.

Resources