I'm getting frequent errors when i start my server. Here is the error:
RangeError: Invalid time value
at Date.toISOString ()
Here is the code:
var start = timestamp;
const expiryDate = (new Date(start)).toISOString().split('T')[0];
This exception occurs when the Date object contains an invalid date.
new Date('undefined').toISOString()
In this example the Date object can be created without any problems, but the toISOString function throws an Error.
To fix your issue, you need to make sure that the timestamp variable contains a valid date string.
In my case endDate had a null value:
const [date, setDate] = useState([
{
startDate: new Date(),
endDate: null,
key: "selection",
},
]);
Changing it to new Date() solved the issue:
const [date, setDate] = useState([
{
startDate: new Date(),
endDate: new Date(),
key: "selection",
},
]);
In my case I had previously used JSON.stringify() on an object Task with an attribute duedate of type Date. Consequently taskObj.duedate became a string and did not work as a Date object in my code.
To fix the error, I converted the stringified date to Date type using taskObj.duedate = new Date(taskObj.duedate);
Related
I have a schema like this
const someSchema = new mongoose.Schema (
{
updatedAt: { type: string }
}, { timestamps: { currentTime: () => moment().format("MM/DD/YY, h:mm") } }
);
I want to query this collection to find the documents updated within a date range for example:
12/05/20 to 12/31/20, hours and minutes don't matter. My startDate and endDate is in the YYYY-MM-DD format. I have tried to find
updatedAt: {
$gte: new Date(startDate),
$lte: new Date(endDate)
}
or
updatedAt: {
$gte: startDate,
$lte: endDate
}
but they are not working. Thank you for your time!
First try to alter moment().format("MM/DD/YY, h:mm")
to moment().format("MM/DD/YY, HH:MM:SS")
then
updatedAt: { $gte: new Date(startDate).setHours(0,0,0,0)
$lte: new Date(endDate).setHours(0,0,0,0)},
You are declaring moment().format as a datetime but when you create Date() object you are passing only the date!
When you are practicing date manipulation better use milliseconds and UTC notation. It is something universal to all type of databases. Avoid to use varchar type to store date as strings.
Give it a try!
I'm trying to read array from Async storage with "reminders" key.
Problem is JSON.parse cannot convert 'time' key of element in Array to Date object.
I need to read from storage, parse and assign to reminders state using setReminders()
// EXAMPLE DATA IN ASYNC STORAGE
[{day: 'Monday', time: '2020-04-03T15:17:07.554Z', status: false},
{day: 'Friday', time: '2020-04-03T15:17:07.951Z', status: true},]
// LOAD REMINDERS
useEffect(readReminders, []);
function readReminders() {
AsyncStorage.getItem('reminders').then(value =>setReminders(value));
}
You can parse Date from string using Date.parse(string) or new Date(string) like:
function readReminders() {
AsyncStorage.getItem('reminders').then(values => {
const reminders = values.map(item => {
return {
...item,
time: new Date(item.time)
}
});
});
}
I have add the same issue with the date . try using moment instead of new Date()...
'npm install moment
import moment from "moment";
const time= '2020-04-03T15:17:07.554Z';
const todate= moment(time);
Hope this will help.
I can not query the collection filtering by a date
The following code is that I have so far
//The following code shows how my schema is:
date: {type: Date, required: true}
//This is a date from a collection in MongoDB:
date: 2019-09-06T16:48:14.000+00:00
//This is how I saved the date in the MongoDB:
"2019/09/09 08:55:15"
//And this is how I perform the query in NodeJS:
let year = req.query.year;
let month = req.query.month;
let day = req.query.day;
let startDate = new Date(year, month, day);
let endDate = new Date(year, month, day);
// find documents in MongoDB
let query = SALES.find({ date: { $gte: startDate, $lte: endDate }});
// execute the query at a later time
query.exec(function (err, item) { // item is a dictionary
if (err) return handleError(err); // throws an error if any
if (item === null || item.length === 0) {
res.json({
status: 'empty',
});
}
else {
res.json({
salesRecord: item
});
}
});
I read that is easy to get, but I am not able to do it. Any help is welcome 🙏
I have not error on the query, simply I get the response as empty.
The expected results is to get the dates from the specified date
Short answer
Date you saved("2019/09/09 08:55:15") is being treated as a string.
Try this:
db.mycollection.find({
"date" : {"$gte": new Date()}
})
or
db.mycollection.find({
"date" : {"$gte": ISODate(new Date())} // current date
});
Description
Mongodb shell provides various methods to return the date.It can be a string or as a Date object:
Date() method which returns the current date as a string.
new Date() constructor which returns a Date object using the ISODate() wrapper.
ISODate() constructor which returns a Date object using the ISODate() wrapper.
I'm a bit new to Vue and was wondering how to get the the past month date: I have this code:
import SomeTable from "./table/SomeTable";
export default {
name: "Cabinets",
components: {SomeTable},
data() {
return {
defaultColumns: [
'id',
'serialNumber'
],
defaultStartDate: new Date(),
defaultEndDate: new Date('2019-10-07')
}
},
props: {
startDate: {type: Date, required: false},
endDate: {type: Date, required: false},
}
}
</script>
And then I put defaultStartDate and defaultEndDate in the SomeTable element as follows:
<some-table :start-date="defaultStartDate" :end-date="defaultEndDate" :default-columns="defaultColumns"></some-table>
Which then returns the correct startDate of today and also the set one. But when i try for instance to do something like this:
defaultEndDate: new Date().getFullYear() + '-' + new Date().getMonth() + '-' + new Date().getDate()
My local environment turns blank and get all sorts of errors. I think this is due to the fact I can't use Javascript in that place in Vue? But again I'm really not yet sure how Vue works and couldn't find much on it by googling. So how could I do this using Javascript or maybe even if Vue has a neat trick for it?
EDIT:
the errors i'm getting are of the form:
Error in data(): "TypeError: Date().getFullYear is not a function"
But then with all the javascript functions i used inside Vue. And also
Invalid prop: type check failed for prop "endDate". Expected Date, got String with value "2019-9-5".
In your codes, endDate: {type: Date, required: false}, means endDate should be in Date type.
So you need to convert calculated value into Date like below:
defaultEndDate: new Date(`${(new Date()).getFullYear()}-${(new Date()).getMonth()}-${(new Date()).getDate()}`)
EDIT:
And you need to think of the month is January(0). By using the above method, you will get error when it is on January.
I think It could be better to use computed value like below;
computed: {
defaultEndDate() {
const now = new Date();
now.setMonth(now.getMonth() - 1);
return new Date(now.toJSON().slice(0, 10));
}
}
I am declaring dateRangePicker field in component state like below
dateRangePicker: {
selection: {
startDate: new Date(),
endDate: new Date(),
key: 'selection',
},
}
later start date and end date changes as below
let startDate = "2019-04-16";
let endDate = "2019-05-16";
But, I am not able to update these value in state after following code block
this.setState({
dateRangePicker.selection.startDate : startDate,
dateRangePicker.selection.endDate : endDate,
})
I want to update the start and end date accordingly
It doesn't work the way you showed. It should be like this:
this.setState(ps=>({
...ps,
dateRangePicker:{
...ps.dateRangePicker, // Copy old data
selection: {
...ps.dateRangePicker.selection, // Same here
startDate: startDate
endDate: endDate
},
}
}))
We use functional form of setState, because you can see at one point we access data from previous state: ps.selection
what your'e tryin to acheive is to change the state of a deep/nested
object in setState..
const startDT = "new start date"
const endDT = "new end date"
this.setState(prevState => ({
...prevState,
dateRangePicker: {
...prevState.dateRangePicker,
selection: {
...prevState.dateRangePicker.selection,
startDate: prevState.startDate = startDT,
endDate: prevState.endDate = endDT,
}
}})
)
Or,
// copy without reference..
const dateRangePicker = { ...this.state.dateRangePicker }
dateRangePicker.selection = {
startDate: startDT,
endDate: endDT,
}
this.setState({ dateRangePicker })
The state is a immutable object, so you can not modified it, but create a new one, so using spread ... expression to create a new state.