Query by date with column that is a datetime - javascript

How can I query for all records of a certain date if the column is a datetime?
This doesn't work. Is there a preferred way to do this in sequelize?
startDate and endDate are datetime's
Thing.findAll({
where: {
startDate = {
[Op.gte]: '02-20-2020'
},
endDate = {
[Op.lte]: '02-20-2020'
},
}
});

Split them and create a javascript Date object before using that straight in the query parameters.
const [month, day, year] = '02-20-2020'.split('-')
const date = new Date(year, month, day)
Thing.findAll({
where: {
startDate = {
[Op.gte]: date
}
}
});
See Several ways to create a Date object

Related

Javascript date increase by a year and subtract a day

I have a date from a vuetify date picker. I take that date and I am trying to format it to MM/DD/YYYY however I need to first add a year to it then subtract one day. So January 1 2022 will become December 31 2022. My code is as follows:
formatDateNextYear(date) {
if (!date) return null
let a = new Date(date);
a.setDate(a.getDate() - 1)
a.setFullYear(a.getFullYear+1)
const [year, month, day] = a.split('-')
return `${month}/${day}/${year}`
}
My issue is this does not seem to add the year and is not formatted correctly.
You forgot to call getFullYear, and instead of trying to format it yourself, you can use toLocaleDateString and pass in the locale you want the format to be in:
function formatDateNextYear(date) {
if (!date) return null;
let a = new Date(date);
a.setDate(a.getDate() - 1);
a.setFullYear(a.getFullYear() + 1);
return a.toLocaleDateString("en-US");
}
console.log(formatDateNextYear(new Date("01/01/2022")));

How to query data from yesterday in moment js or date?

I have data in my database that I want to query thats from yesterday
when i do
momentendYest', moment().startOf('day').subtract(1,'day').toDate()
its today 7am? i dont understand
I'm trying to do something like this :
const yesterdayCoins = await Coin.find({
category: {
$regex: new RegExp(fullName, "i"),
},
createdAt: {
$gte: startYest,
$lt: endYest,
},
what can i do to make the date from yesterday at 12am to 11:59pm ?
Something likes works with plain javascript.
const today = new Date()
const year = today.getFullYear()
const month = today.getMonth()
const day = today.getDate()
const yesterday = new Date(year, month, day-1, 23,59,00)
console.log(yesterday)
We make a date for today, get the year, month and then subtract one from day, then hardcode the time.
I wasn't sure if you wanted at midnight or not. If you do, just skip the time parameters.

Convert Date to UTC with timezone

I'm migrating some data in my DB and have date as a string "date" : "2020-10-23",. I want to convert that date into a datetime object to store it in another field. I'm using luxon
let DateTime = luxon.DateTime;
// Function
function timeZoneToUTC(timezone, year, month, day, hours) {
const dateObj = `${year}-${month}-${day} ${hours}:00`;
let date = DateTime.fromFormat(dateObj, 'yyyy-M-d H:mm', {
zone: timezone
});
return date.toUTC().toString();
}
// Example
let record = {preparationTime: "18",
date : "2020-10-23"};
let dateISO = DateTime.fromISO(record.date, {zone: 'Europe/Amsterdam'});
dateISO = dateISO.minus({days: 1});
let year = dateISO.year;
let month = dateISO.month;
let day = dateISO.day;
record.newOrderTime = timeZoneToUTC('Europe/Amsterdam', year, month, day, 22);
console.log(record.newOrderTime);
<script src = "https://cdn.jsdelivr.net/npm/luxon#1.25.0/build/global/luxon.min.js" > </script>
The code above returns a wrong time, for example: 2020-10-23 will be 2020-10-22T20:00:00.000Z the day is correct however the time is not, it should be 21 instead of 20
This maybe due to Daylight savings times. (https://moment.github.io/luxon/docs/manual/zones.html#dst-weirdness)
To check whether current time is based on DST or not
https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-get-isInDST

How to compare Current System date with with another date

I am getting date in string format from API.
End Date 2014-06-03T06:16:52. I need to write an if-else logic and compare the end date and current date.If end date is less than current date then show customer as In Active and if end date is greater than display the Customer as Active.
I have tried following logic but I am not able to understand and get today's time in string fromat.
this.endDate = this.sampleData != null ?
this.sampleData.customerStartDate : null;
this.currentDate = new Date();
var dd = this.currentDate.getDate();
var mm = this.currentDate.getMonth() + 1;
var yyyy = this.currentDate.getFullYear();
this.currentDate = new Date().toLocaleString()
console.log('End Date', this.endDate);
console.log('Current Date: ', this.currentDate);
if (this.endDate == null) {
this.customerStatus = 'Active';
} else {
this.customerStatus = 'In Active';
}
I am getting current date as Current Date: 4/2/2019, 1:23:34 AM
I want to be able to get in same format as End Date.
My main task is to compare the dates how do I achieve it ?
Ideally you want to clean up the date you're getting from an API, and convert it to a JS Date object. You can do this by keeping only the 2014-06-03T06:16:52 part, and giving it to the new Date() constructor.
You can get the current date by calling new Date() without parameters.
You can the turn the dates in to numbers by calling getTime() on each.
You can then compare the numbers.
const incoming_date = new Date('2014-06-03T06:16:52');
const current_date = new Date();
if (incoming_date.getTime() < current_date.getTime() {
// incoming_date is before current_date
} else {
// current_date is before incoming_date
}
as simple as this:
let date=new Date("2014-06-03T06:16:52")
date>new Date()
you could try to express dates in ms since the Unix Epoch with getTime() and compare them
if (currDate.getTime() > endDate.getTime()) {
// set customer to inactive
} else {
// keep customer active
}
I personally like to use moment() for javascript dates. You really just need to have it compare the same format, so you could have something like:
this.currentDate = moment().toISOString();
const dataDate = this.sampleData ? this.sampleData.customerStartDate : null;
this.endDate = moment(dataDate).toISOString();
if (this.endDate > this.currentDate) {
this.customerStatus = 'Active';
} else {
this.customerStatus = 'Inactive';
}

get an array of dates from today for datepicker with Javascript

I am trying to write a function that returns an array of dates from today till the maximum date, so that I can restrict the date picker selection. At the moment I have the following:-
datesAfterToday: function (date) {
var dates = []
var currentDate = new Date()
var endDate = new Date(8640000000000000).getFullYear()
var addDays = function (days) {
var date = new Date(this.valueOf())
date.setDate(date.getDate() + days)
return date
}
while (currentDate <= endDate) {
dates.push(currentDate)
currentDate = addDays.call(currentDate, 1)
}
return dates
}
and then I am using Vue.js to mount it as follows :-
mounted () {
this.allowedDates = this.datesAfterToday
},
however I am only getting an array of objects instead of the proper array.
How can I get the proper array of dates so that I can bind it to the allowdates property.
Thanks for your help and time!
For starters new Date(8640000000000000).getFullYear() will set endDate to the year of that date, which is 275760. currentDate will be today's date (in milliseconds), which at the time of me writing is 1511272934156. As you can see currentDate is always greater than endDate, so your while loop never goes to the statements inside.
Another issue is that the date you picked is really far in the future and you're populating an array one day at a time. Your loop will most likely make the page freeze or crash completely. Try picking a date that's more manageable.
For instance, in the snippet below I set endDate by first initializing it to today, then setting the year to exactly one year from now. This gives me an array with roughly 365 values.
You can imagine how big this array would be if I used a year that was 273,748 years in the future.
var dates = []
var currentDate = new Date()
var endDate = new Date()
endDate.setFullYear(endDate.getFullYear()+1)
var addDays = function (days) {
var date = new Date(this.valueOf())
date.setDate(date.getDate() + days)
return date
}
while (currentDate <= endDate) {
dates.push(currentDate)
currentDate = addDays.call(currentDate, 1)
}
console.log(dates)
With all that being said, it looks like you're actually allowed to pass an object specifying the minimum and maximum values rather than an array.
https://vuetifyjs.com/components/pickers#example-6
let d = new Date() // today
let d2 = new Date()
d2.setFullYear(date.getFullYear()+1) // Next year
this.allowedDays = {
min : d.toISOString().substr(0, 10), // e.g. 2017-11-21
max : d2.toISOString().substr(0, 10)
}
Another option would be to use vuejs-datepicker For example:
<script>
var state = {
disabled: {
to: new Date(), // Disable all dates up to specific date
from: new Date(8640000000000000) // Disable all dates after specific date
}
}
</script>
<datepicker :disabled="state.disabled"></datepicker>
See Disabled Dates in the documentation.

Categories