Can't cast date to a string formatted date in mongoose - node.js

I want to format the date object to a string date format - However, I get a cast error:
Cast to date failed for value "11/2020" at path "startdate"
The start date property is from type Date:
testSchema = new Mongoose.Schema({
startdate: {
type: Date
});
It's saved as type date in the collection - but later I need to get this object entry and change the startdate to a formatted string for further processing.
I tried it as following:
myObject.startdate = moment(myObject.startdate).format('MM/YYYY');
Then I get the cast error.
Is it even possible to achieve what I want? If no, how should I handle this then?

I just created a new array entry and wrote the formatted string into it. Couldn't find solution for the problem

Related

Convert string to date format in azure data factory using set variable

I have string date in set variable "20211222"
And I want to convert it into date like 2021-12-22.
And I have use this function in variable set dynamic content
#formatDateTime('20211222', 'yyyy-MM-dd')
But error occur In function 'formatDateTime', the value provided for date time string '20211222' was not valid. The datetime string must match ISO 8601 format
Is there any other function to convert this string "20211222" into date?
Actually the string '20211222' is already in the unambiguous format of YYYYMMDD and will always be interpreted this way. If you need to use this string input as a date, just do a cast:
SELECT CAST('20211222' AS date); -- 2021-12-22
If you wanted to formerly go from your text input YYYYMMDD to a text output of YYYY-MM-DD, then make a round trip:
SELECT CONVERT(varchar(10), CAST('20211222' AS date), 120);
Function formatDateTime expects "a string that contains the timestamp".
Example:
formatDateTime('03/15/2018 12:00:00', 'yyyy-MM-ddTHH:mm:ss')
You would have to manage to input in a timestamp format. The default format for the timestamp is "o" (yyyy-MM-ddTHH:mm:ss:fffffffK), which complies with ISO 8601 and preserves time zone information.
Please use the below logic:
#concat(substring(pipeline().parameters.Test,0,4 ),'-',substring(pipeline().parameters.Test,4,2),'-',substring(pipeline().parameters.Test,6,2))

MongoDB script to convert String "09-Jan-20" // (DD-MMM-YY) into ISO Date

I have multiple records in a collection and date is stored as String as "09-Jan-20". I need to convert this date into ISODate by script execute on MongoDB.
exp: "09-Jan-20" to ISODate("2020-01-09T00:00:00.000Z").
Anyone, please help.
Got the solution for this question.
We can use mongo's $convert to convert the string into date format.
Exp:
"$convert":{
input: "$dateField",
to: "date"
}
We can also find out the records where date is in string format by-
"$type":"string"
Refrence:- mongo type check and mongo convert

How can I store String type DateTime string to ISO DateTime Format in MongoDB in NodeJS?

I am receiving a string format DateTime Strings data from the android app.
according to the previous developer's note, it's RFC 3339 format string.
I am stuck in converting this string into MongoDB's Date format in Node.js
in detail edit documents, formats are like below.
[ current string what I receive -> Date format in MongoDB, which I wanna store]
[ "2020-09-14 08:18:56", -> ISODate("2020-09-14T08:42:41.000Z") ]
Is there any way to save those strings into MongoDB on NodeJS programmatically?
RFC 3339 is a profile (a set of standards derived) of ISO 6081 (see these SO posts: [1], [2]), so you can just pass the string to Date's constructor and get a valid js Date:
var raw = "2020-09-14 08:18:56";
var date = new Date(raw);
console.log(date);

Convert Excel Date string to Unix time in Node.js

I use node-xlsx for parsing Excel file in Node.js but Date cells have strange view before parsing:
in Excel: 01.03.2016 07:44:04
in Node.js before parsing: 42430.32226851852
How I can convert this string 42430.32226851852 to Unix-time format?
Update:
Write myself solution that work for me:
var xlsxDate = '42430.32226851852'
var splitDate=xlsxDate.split('.');
var unixTime=new Date(1900, 0, splitDate[0]-1, 0, 0, Math.round(splitDate[1]/1157410)).getTime()/1000
console.log(unixTime)
Where 1157410 is ~1 second
i use this code to convert Unix-time format:
date = new Date(( parseInt(excelDate) - (25567 + 2))*86400*1000)
unix = date.getTime()
Another work around is that when you save it to your excel document you save it as a string and not a Date object.
Then you can read it as a string and just wrap it in a new Date()

Removing time format in a date

I am passing a date as a parameter where daydate is the parameter
string daydate
The content of daydate is 3/3/2017 12:00:00 AM
I am doing this to remove the time and still the time does not go away
string strCleanDate = String.Format("{0:M/d/yyyy}", daydate);
How can I remove the time?
That is because the formatter is ignored as the instance you are passing to string.Format is a string so the placeholder is filled with the value you passed in and the format is ignored completely. If you want to apply DateTime formatting you need to pass in an instance of a DateTime type.
Your first fix should be to change your parameter to be of type DateTime and not string. Otherwise a caller could pass "HI THERE" and your method/application would break, or worse push that value to a store and now your store is polluted with invalid value(s).
public void SomeMethod(DateTime daydate)
{
var dateOnly = dayDate.Date;
}
You should pass dayDate as a DateTime value instead of as a string. Then the format will work correctly.

Resources