Mongoose/MongoDB regex find query using variable - regex

I'm trying to perform a find query in mongoDB (using the mongoose framework with nodejs and express) using a regex expression based on a variable. I'm partially able to query correctly using a static string hard-coded into the code but I need to perform the query using a variable who's value is constantly changing.
The query uses three fields (author, date and updated date) and should go through all documents in a scheme and find all documents where 'date' or 'updated date' is like the variable currentDate (always formatted as YYYY-mm-dd) and where author is authorname.
The main issue is that mongoDB stores the dates with ISO format. On execution the string variable (currentDate (YYYY-mm-dd)) is formatted to ISO which leads it to only "hit" documents with time 00:00.000Z as ISO formats 2015-09-17 to be 2015-09-17 00:00:00.000Z (not taking timezone into consideration).
The query:
var currentDate = yyyy-mm-dd;
Scheme.find({$and:[{'local.author': author},
{$or: [{'local.date': new RegExp(currentDate)},
{'local.updated_date':new RegExp(currentDate)}]}]}
I've also tried with the $gte and $lte variables (where $gte = date today and &lte = date tomorrow (00:00)) but I couldn't get it to work, I met the wall trying to perform the regex.
I hope there is a brilliant regex expert out there who can help, thank you! :)

I figured it out without all the complexity.
var tomorrow = new Date();
var today = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0,0,0,0);
today.setHours(0,0,0,0);
Scheme.find({$and:[{'local.author': author},{$or: [{'local.date': { $gt: today, $lt: tomorrow } },{'local.updated_date':{ $gte: today, $lt: tomorrow }}]}]}

Related

CFSpreadsheet not formatting dates

I think I have read just about every post on this topic and none of the proposed solutions works in my case so here goes.
I am using CF9 (upgrade not an option) for this project. I query a date field from a MSSQL database and use spreadsheetAddRows() to put the results into a spreadsheet (xls or xlsx, same result either way).
The date shows in excel as 2020-05-11 00:00:00.0 and isn't recognised as a date so the date formatting doesn't work.
I have tried using SpreadsheetFormatColumn (s, { dataformat="d-mmm-yy" }, 2); but this doesn't format the date either and has the exact same result in excel.
I have tried many variations of selecting convert(varchar, datecolumn, 101) from the database but these always just end up as text fields in excel as well so again, no date formatting and they sort in the wrong order.
Can anyone tell me what the correct format for a date is for CFSpreadsheet so that excel actually recognises it as a date?

How to force Power BI service to use Local timezone

I'm currently exporting data from Dynamics. There are a lot of date fields I need to export. By default they are all UTC timezone. I want to report on New Zealand timezone. I tried to approaches in Power Query:
1) use DateTimeZone.ToLocal: =Table.TransformColumns(#"dataset", {{"**UTC Date**", **DateTimeZone.ToLocal**, type datetimezone}})
2) use a specific timezone ("en-NZ"): =Table.TransformColumnTypes(#"dataset", {{"UTC Date", type datetime}}, **"en-NZ"**)
Both approaches work OK in Power BI desktop report, However once I published to Power BI service and after several refreshes (initially it was NZ time), the time turn back to UTC time.
I don't want to create extra columns in DAX and really want to try use Power Query. Is there any way to work it out?
I have faced the same issue a while back and came up with the following solution.
You can simply use a combination of RemoveZone and Addzone power query function to achieve this. Example below shows how to get Indian Standard Time(IST = +5:30)
= DateTime.AddZone(DateTimeZone.RemoveZone(DateTimeZone.UtcNow()),5,30)
Note that I used DateTimeZone.UtcNow() to always get the Universal standard DateTime and then convert this to the Indian time. You can use your own timezone values
Add the above code in place of DateTimeZone.ToLocal in your code.
Documentation to DateTime.AddZone
Power BI:
Use this Power Query function to convert times from UTC to Local Time, here its mountain time, but you can set to Whatever New Zealand Time is
let
ConvertDateColumnstoMountainTime = (sourcetable as table) as table =>
let
TargetColumnList = Table.ColumnsOfType(sourcetable, {type datetime, type nullable datetime, type datetimezone, type nullable datetimezone}),
AdjustTimeZones = Table.TransformColumns(sourcetable, List.Transform(TargetColumnList,
(name) => {name, (date) => if date <> null
then DateTime.From(DateTimeZone.RemoveZone(date) + #duration(0,CalculateUTCOffset(date,null),0,0))
else null})),
//Above returns type as text. Need it explicitly as Date.
AdjustColumnTypes = Table.TransformColumnTypes(AdjustTimeZones,List.Transform(TargetColumnList,
(name) => {name, type datetime}))
in AdjustColumnTypes
in ConvertDateColumnstoMountainTime

Date comparison in power bi dax

I had a problem with my code. It returns the below error:
DAX comparison operates do not support comparing values of type date with values of type text.
Basically, I want to count rows based on some conditions. And I know there is a need to convert the data type, but I am not sure how to do it.
Total Open Issues =
--------------------
--basic info
VAR SELECTEDDATE =
DATEVALUE(SELECTEDVALUE(Calender[FullDateAlternateKey].[Date]))
--------------------
--FIND the relvent data
VAR rlvttable =
calculatetable(
Tracker,
Tracker[Catagory]="ISSUE",
DATEVALUE(Tracker[ClosedDate])>SELECTEDDATE
||Tracker[ClosedDate]=""
)
--------------------
--Results
Return
countrows(rlvttable)
Anyone could advise me how to correct it? Thanks~
Check the data type of columns Tracker[ClosedDate] and Calender[FullDateAlternateKey] - one of them is Text, rather than Date.
To fix, you could:
choose a different field which is already a Date format
change the format of the offending column
use DATEVALUE in your measure, to convert the text date to a real date.
It also looks like you need to edit this statement, as these conditions conflict:
Tracker[ClosedDate]>SELECTEDDATE
&&Tracker[ClosedDate]=""
I am trying to compare the closedDate with "". I should use blank() instead.

Querying embedded documents by matching dates in MongoDB

I have a database containing emails and I want to search for a specific date using the regex operator and return all emails sent on that date. I have tried this so far but it doesn't return anything. I am new to regex and am not sure if I'm querying the date correctly.
db.messages.find({'headers.Date' : $regex : '2001-07-06'}})
This example below successfully returned all the emails send from the specified email address.
db.messages.find({'headers.From' : { $regex : 'reservations#merriotts.com' } });
The emails contain the following information:
headers { content transfer encoding, content type, date, from, message id, mime version, subject, to }
You need not make use of regex here, something simpler like this should work:
db.posts.find({"headers.Date": new Date(2001, 06, 06) })
This should work if the dates you saved in the DB are without time (just day, month, year)
Now if you have dates saved with new Date(), which includes the time components as well, then you need to create a date range which includes all the moments for that day :
db.posts.find( //query for all moments/time of a specific date
{"headers.Date": {"$gte": new Date(2001, 6, 6), "$lt": new Date(2001, 6, 7)}})
Note - The API for Date is Date(YYYY,MM,DD) and counting for 'month' starts from '0' and counting for 'date' starts from '1'.

jquery datepicker returns wrong year when using date format without year part

I use jquery datepicker (v1.8.20) with the date format set to 'D MM d' which looks like: 'Wed September 12'. When I select different year than the current one from a calendar, the getDate method returns me current year. Moreover, when I open calendar again, both selected day and month are preserved, but the year is changed for the current one. Generally it looks like year is set to current one when the date format does not contain it. Any idea how to fix that?
Datepicker stores value only in input element, so if you don't have a year in format string, datepicker merely doesn't store it. Here one of jquery.ui developers says, that this is not a bug and "Datepicker is only designed to pick a full date".
Anyway, I had the same problem in my project, and solved it by ugly trick, that forces datepicker to store full date:
(function($) {
$.datepicker._selectDateParent = $.datepicker._selectDate;
$.datepicker._adjustInstDateParent = $.datepicker._adjustInstDate;
$.datepicker._adjustInstDate = function(inst, offset, period) {
var fullDate = inst.input.data('fullDate');
if(fullDate && !period) {
inst.drawYear = inst.currentYear = fullDate.getFullYear();
}
this._adjustInstDateParent(inst, offset, period);
};
$.datepicker._selectDate = function(id, dateStr) {
var target = $(id);
var inst = this._getInst(target[0]);
var date = this._daylightSavingAdjust(new Date(inst.currentYear, inst.currentMonth, inst.currentDay));
inst.input.data('fullDate', date);
this._selectDateParent(id, dateStr);
}
})(jQuery);
I've tested it only with datepicker 1.9.0+ and I'm not sure, that this is a good solution, but it works for me :)