I am trying to return search results for every document that was created on a day. Below is the query I use.
var query = Document.find({}).populate('contacts');
var gte = moment(req.query.date, 'DD-MM-YYYY').startOf('Day');
var lte = moment(req.query.date, 'DD-MM-YYYY').endOf('Day');
query.where('dates.createdAt').gte(gte).lt(lte);
This query works for some days but not all. I can't seem to understand the behaviour. Please help out.
The format of the date in the query string is DD/MM/YYYY.
Works for : 2016-04-16T00:02:30.065Z
Does not work for: 2016-04-15T19:02:59.758Z
It's wrong because you don't initialize as .utc(), and MongoDB dates are stored in UTC:
var gte = moment.utc(req.query.date, 'DD-MM-YYYY');
var lte = moment.utc(req.query.date, 'DD-MM-YYYY').endOf('Day');
And there is no need for the startOf() either.
If you don't construct like that, then the resulting Date object is skewed by the difference in the local timezone. Hence why you don't see the selection working where the hours would cross over dates.
Also if dates are coming in as 01/01/2016 then the format string would be 'DD/MM/YYYY', but one or the other is likely a typo in your question.
Related
We are trying in a RESTLet to access the sublist "demandplandetail" from a NetSuite Item Demand Plan. Everything goes fine until a certain point. We are able to load it and process the demandplan for 2020. However, here it gets frustrating.
We know (can see from NetSuite) that there is data also for 2021. However, to access that from SuiteScript seems very difficult.
1st solution) The item demand plan has the field "year". OK, just set that to 2021, save and reload the record. Result: saving ignored, year still is 2020.
2nd solution) Set the year using a Date object as in:
var demandPlan = record.load(...)
var d = new Date();
demandPlan.setValue({
fieldId: 'year',
value: d
});
Gives the following:
:"TypeError: Cannot find function getFullYear in object NaN. (NLRecordScripting.scriptInit$lib#59)","stack":["setDatesForMonthAndYear(NLRecordScripting.scriptInit:108)","anonymous(N/serverRecordService)"
on saving the record. I also get the same using (various) strings adhering to acceptable date formats (as in '1/1/2021'). I have also tried the format package giving me a date string -> the same result.
Also read somewhere that you may need to set the start date (field 'startdate') in the record. Tried several variations but it stubbornly refuses :(.
Wonder if anyone has seen anything similar?
Best Regards,
Toni
Hi Please try the below code also check if you're passing date object to the field not the date string.
function formatDate() {
var dateROBD = format.parse({
value: new Date(),
type: format.Type.DATE
});
// this line optional if you want to try with or else ignore this
dateROBD = convertUTCDateToLocalDate(new Date(dateROBD));
return dateROBD;
}
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
OK, mystery solved. Turned out that this is not supported in SuiteScript 2.0 but you need to use 1.0.
I am working on importing data in an Excel file and it has date column.
In my app, that date column value comes as a serial number like 43101.622083333335 which stands for 01/01/2018.
When converting this serial number from Excel back to the normal date it stands for, it gives wrong year.
For example, it gives 01-Jan-1970 instead of 01-Jan-2018
``
var moment = require('moment');
var excelDate = 43101.622083333335;
var date = moment(new Date(excelDate));
var dateWithNewFormat = date.format('DD-MMM-YYYY');
console.log(dateWithNewFormat);
``
Output: 01-Jan-1970 instead of 01-Jan-2018
Any help ?
Thanks in advance.
I don't think this is an issue with the moment library. It seems that you aren't calling Date with a valid constructor argument with new Date(excelDate) (see official documentation for Date here).
The Date class doesn't understand the concept of 'Excel time' but it does understand the concept of a unix timestamp. If you refer to this post, you can see how to convert from Excel time to a unix timestamp, depending on which version of Excel you are using.
Then, I would change your code to:
var moment = require('moment');
var excelDate = 43101.622083333335;
var unixTimestamp = (excelDate-25569)*86400 //as per the post above, convert Excel date to unix timestamp, assuming Mac/Windows Excel 2011 onwards
var date = moment(new Date(unixTimestamp)); //Pass in unix timestamp instead of Excel date
var dateWithNewFormat = date.format('DD-MMM-YYYY');
console.log(dateWithNewFormat);
I'm trying to get all staff members within a given date range using mongoose ODM, but can't seem to find a way.
I tried using different date formats, but came up with storing ISO date in my db. Now it saves and retrieves dates as ISODate("2018-12-23T00:00:00Z") format.
But, what I want is to get all staff members using a date range given using $gte and $lte
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.staff_id, date:{$gte:params.frm, $lte:params.to}},callback);
}
This gives nothing but this gives all staff members who signed that day
Model.find({date:'2018-12-22'},callback);
That's because your params are sending date+time, whereas gte and lte only take date. The output of your log console.log(new Date(params.frm).toISOString()); should show timestamp
A foolish question. Pardon me..
I actually found what was wrong with it.. It was a friggin TYPO
here is it we can implement this using different approaches
since we store only a date, the mongoose will automatically convert all values we provide to ISODate format (date+time). Its actually very good because a similar pattern for a date or a time. So simply we can use the above code i have given It will work fine
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.id, date:{$gte:params.frm, $lte:params.to}},callback);
}
actually instead of staff_id: params.staff_id, all I had to do was params.id
because thats how I defined the staff_id in http GET req. which is /staff/:id/:frm/:to
anyways we can even use where to do this as well a different approach...
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(params.id);
AttendanceStaff.find({date:{$gte:params.frm, $lte:params.to}}).sort({date:-1}).where({staff_id:params.id}).exec(callback);
}
so that's it ...
I have document contains properties like this
{
"id":"1bd13f8f-b56a-48cb-9b49-7fc4d88beeac",
"name":"Sam",
"createdOnDateTime": "2018-07-23T12:47:42.6407069Z"
}
I want to query a document on basis of createdOnDateTime which is stored as string.
query e.g. -
SELECT * FROM c where c.createdOnDateTime>='2018-07-23' AND c.createdOnDateTime<='2018-07-23'
This will return all documents which are created on that day.
I am providing date value from date selector which gives only date without time so, it gives me problem while comparing date.
Is there any way to remove time from createdOnDateTime property or is there any other way to achieve this?
CosmosDB clients are storing timestamps in ISO8601 format and one of the good reasons to do so is that its lexicographical order matches the flow of time. Meaning - you can sort and compare those strings and get them ordered by time they represent.
So in this case you don't need to remove time components just modify the passed in parameters to get the result you need. If you want all entries from entire date of 2018-07-23 then you can use query:
SELECT * FROM c
WHERE c.createdOnDateTime >= '2018-07-23'
AND c.createdOnDateTime < '2018-07-24'
Please note that this query can use a RANGE index on createdOnDateTime.
Please use User Defined Function to implement your requirement, no need to update createdOnDateTime property.
UDF:
function con(date){
var myDate = new Date(date);
var month = myDate.getMonth()+1;
if(month<10){
month = "0"+month;
}
return myDate.getFullYear()+"-"+month+"-"+myDate.getDate();
}
SQL:
SELECT c.id,c.createdOnDateTime FROM c where udf.con(c.createdOnDateTime)>='2018-07-23' AND udf.con(c.createdOnDateTime)<='2018-07-23'
Output :
Hope it helps you.
I am using moment.js to work with dates and times in node.js. So far I've been able to do everything I need with it, but I am having problems formatting a time.
Here's the scenario:
User enters data (an integer), which is logged in a database, along with date (in the format YYYY-MM-DD) and time (in the format HH:MM:SS).
Next time the user goes to enter data, the previous value is read in and compared (higher, lower or equal to) the new value. However I also want to display a message such as "The last time you submitted your data was at TIME on DATE". In this case, I'd like time to be displayed in a different format (e.g. "h:mm a" i.e. "12:34 pm").
Can I use moment to format an existing date, or can moment only return current date/time? In my code I have the following function:
function userFormattedTime(time)
{
let uTime = moment(time).format('h:mm a');
return uTime
}
However when I call this function and pass it the time (taken from the database), I get "Invalid Time". What am I doing wrong?
You would parse the string from a string back to a moment object, then you can use moment to reformat the date into any other format.
I guess what you are doing wrong is not telling moment what you're sending it back, i.e. it doesn't understand the formatted string you're supplying.
Notice the format values HH:mm:ss which vary in case. The case is important and should be set to match your requirements. https://momentjs.com/docs/#/parsing/
// Original date time string
var rawDateTime = "02-02-2018 10:20:30";
// convert string to a moment object
var originalDate = moment(rawDateTime, "MM-DD-YYYY HH:mm:ss");
// Format a new string from the moment object
var newFormattedString = originalDate.format('h:mm a');
In order to calculate the difference of moment objects you can use the diff function. https://momentjs.com/docs/#/displaying/difference/
// Two different dates
var dateOne = moment("02-02-2018 10:20:30", "MM-DD-YYYY HH:mm:ss");
var dateTwo = moment("04-04-2018 10:20:30", "MM-DD-YYYY HH:mm:ss");
// Get the difference of the two dates
var diff = dateOne.diff(dateTwo);