Detect SQL min date string in javascript - javascript

In an angularJS application I'm working on, we are using a SQL server to provide data. Nothing odd there. I am tasked with working on the date sanitation, so objects that are passed into our application that have the SQL min date value are undefined, and ones that do have a date are javascript Date objects.
I know that during upgrades, sometimes the string that is defined for min date can change. Right now, it is 0001-01-01T00:00:00Z. I cannot just do a string comparison because it could change.
My problem is, how do i determine this min date in javascript? new Date(varThatIsMinDate) is a valid date. MomentJS thinks its valid too, and it is technically a valid date. I'm less worried about validation and more worried about "is it min date"
How do you determine min date === true from a zulu pattern 0001-01-01T00:00:00Z or similar in javascript?
My code so far, cause I know you are gonna ask for it:
if (response.config.responseType === 'json') {
for (var property in response.data) {
if (response.data.hasOwnProperty(property)) {
if (property.toUpperCase().indexOf('DATE') > -1 || property.toUpperCase().indexOf('TIME') > -1) {
console.log(property.toUpperCase());
// Attempt to use JS built in date and validate
var tmpDate = new Date(response.data[property]);
if ($moment(tmpDate).isValid()) {
// Make it a valid date object if it has a valid date
response.data[property] = tmpDate;
} else {
// Make it undefined
response.data[property] = undefined;
}
}
}
}
}
Thing is, its always valid, so this code doesn't work.
EDIT: I could cheat and use tmpDate.getUTCFullYear() === 1 but i'd like to know how to do this the right way.

Related

How to check if the date is not valid in Typescript

I want to check if the date is not valid in typescript, looking through some of the answers based on javascript. This is the result I came across but unfortunately, it is not working.
...
// Check if the given date is not valid
else if (!(giftCards[i].expiry instanceof Date) || isNaN(Number(giftCards[i].expiry)))
return new Result(true, ErrorCode.BadRequest, 'Expiry Date out of range', {
index: i,
giftCard: giftCards[i]
});
...
How to check if the date is not valid in Typescript
You are trying to check the runtime variable if it's a valid Date object, which is not the best idea. I would just use javascript to check it.
You could either use e.g. isDate function from lodash (https://www.npmjs.com/package/lodash.isdate) or check it manually (and this is what I'm doing with dates in my projects):
const isDate = (value: unknown): value is Date => {
return value instanceof Date && !isNaN(+value);
}
Note: your code sample probably should work fine, but you should replace || with && in your condition.

I am having trouble with the logic flow of this code, and the resulting incorrect output

I am trying to build a graph based on BTC historical price data from coinbase.
Part of this requires that I make the data retrieved from coinbase usable for chart js.
I am having trouble with this aspect.
function getBitcoinHistory(){
$.ajax({
url: "https://api.coindesk.com/v1/bpi/historical/close.json",
success: function(historicalPrice){
console.log(JSON.parse(historicalPrice))
var dateArray = []
dateToday = year+"-"+month+"-"+day
dataStartDate = year+"-"+month-1+"-"+day-1
console.log("month is "+month+" day is "+day)
//set up month and day first
dataMonth=month-1
dataDay=day-1
console.log("data date starts at 0"+dataMonth+"-"+dataDay)
//loop through all of the dates from oldest to newest
for(var i = 0; i < 31; i++){
//formatting correctly
if(dataMonth<10 && dataDay<10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-0"+(dataMonth)+"-0"+(dataDay+i)]; console.log("2019-0"+(dataMonth)+"-0"+(dataDay+i))}
if(dataMonth<10 && dataDay>=10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-0"+(dataMonth)+"-"+(dataDay+i)]; console.log("2019-0"+(dataMonth)+"-"+(dataDay+i))}
if(dataMonth>=10 && dataDay<10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-"+(dataMonth)+"-0"+(dataDay+i)]; console.log("2019-"+(dataMonth)+"-0"+(dataDay+i))}
if(dataMonth>=10 && dataDay>=10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-"+(dataMonth)+"-"+(dataDay+i)]; console.log("2019-"+(dataMonth)+"-"+(dataDay+i))}
//if the date does not exist then it must be the next month
if(dateArray[i]==undefined){
dataMonth=dataMonth+1
dataDay=1
if(month>12){
month=1
}
if(dataMonth<10 && dataDay<10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-0"+(dataMonth)+"-0"+(dataDay+i)]; console.log("2019-0"+(dataMonth)+"-0"+(dataDay+i))}
if(dataMonth<10 && dataDay>=10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-0"+(dataMonth)+"-"+(dataDay+i)]; console.log("2019-0"+(dataMonth)+"-"+(dataDay+i))}
if(dataMonth>=10 && dataDay<10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-"+(dataMonth)+"-0"+(dataDay+i)]; console.log("2019-"+(dataMonth)+"-0"+(dataDay+i))}
if(dataMonth>=10 && dataDay>=10){dateArray[i]=JSON.parse(historicalPrice).bpi["2019-"+(dataMonth)+"-"+(dataDay+i)]; console.log("2019-"+(dataMonth)+"-"+(dataDay+i))}
}
console.log("Data at "+i+" is: "+dateArray[i])
//console.log("2019-"+(month-1)+"-"+(day+i))
}
}
})
I am hoping to have the dates be stored in an array in the correct format, but instead everything gets very weird at the point when the month changes. It will skip a few days and then also start adding extra 0s to the front of numbers even when it shouldnt...
I am lost.
Thank you for you time.
As some have noted in the comments, there seems to be a bit of a readability issue, especially with all this date parsing / formatting. I would strongly consider looking into moment.js (or similar), where the primary date format you seem to desire is essentially iso8601 (so parsing too and from this format should be particularly painless!) :)
https://momentjs.com/docs/#/parsing/special-formats/
Nested ifs are generally undesired (if you can split the contents of an if into a separate function, then you should usually do so)
Your code is expecting everymonth has 31 days, you need to contrl that and im sure it will perform different at the moment it goes on for the next month :/

Mongoose, Function Cannot Be Performed on Undefined, From Query Function

I am trying to use today's date as the field on which to
search - at this juncture, without having to worry about integrating the moment.js library. There will be only one record with today's date in the "table".
app.dashboardreport
.findOne()
.$where(function() {
return this.createdOn.toJSON().slice(0, 10) == Date.now().toJOSN().slice(0, 10);
})
.exec(function(err, foundDashboardReport) {
The $where function is throwing an error.
MongoError: TypeError: Cannot call method 'toJSON' of undefined
at _funcs1 (_funcs1:2:26) near 'createdOn.toJSON().slice(0, 10) == Date.n' (line 2)
It appears that "this" may not be defined; however, I know there is a record which meets those criteria.
Am I structuring the query incorrectly? If so, what needs to change in the function so that I can filter on that condition?
The problem, here, is a lack of understanding of the way in which data is structured in document-based storage. Each document must contain the createdOn field - if it is absent, then you will receive this error. this is defined; createdOn is not.
If you have data that is not uniform, you need to evaluate the presence of the field. So, it might look like this:
app.dashboardreport
.findOne()
.$where(function() {
/* The check for the field createdOn ensures that the compared document
does indeed contain the field */
return this.createdOn && this.createdOn.toDateString() == new Date().toDateString();
})
Note the addition of the this.createdOn evaluation in the return statement.
You get this error a CreatedOn is not present in every document. You could so something like this to get around this:
(this.createdOn|| new Date('31 Dec 9999')).toJSON()

Date range picker - invalid date

I am using this date range picke (http://www.daterangepicker.com/#options)
Here are some of options, I understand the most but I need help about "isInvalidDate"
I use this code and it works perfectly. Date 11/12/2015 is disabled and users can't select it.
isInvalidDate: function(date) {
if (date.format('YYYY-MM-DD') == '2015-11-12') {
return true;
} else {
return false;
}
},
But I need to add few dates to invalid, so user can't use them.
I don't know how to do some array and loop through to return true or false days, could anyone help me with this?
I hope it will help someone
var some_date_range = [
'02-04-2016',
'03-04-2016',
'04-04-2016',
'05-04-2016'
];
"isInvalidDate" : function(date){
for(var ii = 0; ii < some_date_range.length; ii++){
if (date.format('DD-MM-YYYY') == some_date_range[ii]){
return true;
}
}
}
You'll need a way to feed the blocked date from your backend to the client.
But let's suppose you solved that and have the dates in an array.
All you need to do then is to check if the date in in the array.
See e.g. here:
Checking if date belongs to array of dates
To get the invalid dates from the backend you could either put them in the script itself, of have the script fetch the to be blocked dates from the server using e.g. ajax.
Don't forget to revalidate on the server, never trust filtering in the clients to actually happen.

Function with If/Else Statement - Comparing Dates

I am working with a function and I am trying to write an if-else statment that compares a textfield aganist some dates.
This is what my function looks like right now:
function updateSwitch(){
if (effDate.getValue() > effDate.getValue(new Date())){
submitButton.disable();
}
else{
submitButton.enable();
}
}
I am trying to compare the dates among the same textfield. I am trying to say that if the date enetered in the textfield is in the future (any day after tomorrow), then disable the submitButton. I know that
the function works if I compare the textfield aganist a different textfield, but I cannot get it to work within the same textfield.
I'm not sure how you have your dates constructed. Is effdate been constructed with the Date object? If so, may I suggest using valueOf(). It'll return the primitive value - or the date in milliseconds. This should allow for a more consistent comparison.
function updateSwitch(date){
var effDate = Date.parse(date);
var curDate = new Date();
if (effDate.valueOf() > curDate){
submitButton.disable();
}
else{
submitButton.enable();
}
}

Categories

Resources