Hey
Just wanted to share some functions I made for getting first and last days of a month and also monday and sunday of a week. Just send any date to the functions and they will return dates for you.
function getFirstDay(d){ // Returns date of first day in month var dt = new Date(d); return new Date(dt.getFullYear(), dt.getMonth(), 1); } function getLastDay(d){ // Returns date of last day in month var dt = new Date(d); return new Date(dt.getFullYear(), dt.getMonth() + 1, 0); } function getMonday(d) { // Returns date of monday in the week of date you send to the function d = new Date(d); var day = d.getDay(), diff = d.getDate() - day + (day === 0 ? -7 : 0); return new Date(d.setDate(diff)); } function getSunday(d) { // Returns date of sunday in the week of date you send to the function d = new Date(d); var day = d.getDay()-7, diff = d.getDate() - day; return new Date(d.setDate(diff)); }
They are nice to have when you have to calculate range in dates before making a data collection query.
Thank you Andreas, this is really helpfull.
Â
But, what about date increment? how can I add 30 Days to the current date? I have problems with February and with months with 31 days. I ended up doing an extremely large function not too clean to the eyes. I'm sure there must be a straightforward solution.
Â
Any help will be very much appreciated
Thank you!
I will look into that later
I knew there would be an easy way to do arithmetic operations with dates, actually pretty simple:
Â
var TodaysDate = new Date();
TodaysDate.setMonth(TodaysDate.getMonth()+1);
Â
There is a complete set of Methods for date objects specifically designed to perform arithmetic operations with dates.
Â
Thanks for the reply
Â
Â
Â
Is there a way to calculate a value based on a fixed start date and the current date?
Hi Jerry,
Â
What value do you want? If it's the difference between two dates, then it would be something like this:
For different date calculations, you can do an Internet search and find gobs of useful stuff.
Â
I hope this helps,
Â
Yisrael
I am having a problem with your code Yisrael. Its hows there is an error with last bracket. Here is smy code;
Â
import wixData from 'wix-data'; $w.onReady(function () { $w("#dataset3").onReady( () => { var date1 = itemData._createdtedDate; var date2 = new Date(); // today var timeDiff = Math.abs(date2.getTime() - date1.getTime()); // get time diff in milliseconds var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); $w("#dataset3").onReady( () => { { if(timeDays > 0) { $w("#text16").text = "40" } else { let daysLeft =40* timeDays; $w("#text16").text = daysLeft.toString(); } }
I want to get the total records less than 12 hours as well as more than 12 hours and less than 24 hours and records more than 24 hours.
Note . I have a data containing requests and recorded by dates
Can you help me by asking an example
make the total im textbox
This should get your started:
Â
JavaScript getTime() Method
Â
Use something like this to establish your boundaries:
Â
Then do a query on your records with these boundaries:
Query between values
Â
Something like:
Â
wooow .... this is another code I look
nice code I will use in another process
thank brainstorrrm
Â
Â
But my question is how late orders are calculated
Example: I have orders that are either closed or still open
I would like to know the count number of orders that are still open and not closed
The method you mentioned is a query by today's date
While I want to query based on the date of the orders ( use order date in my data )
count : Order date + 12 hours?
count : Order date more than 12 hours and less than 24 hours
count : Order date more than 24 hours
my data collection name : data937
date field : orderdate
status field : status
and add the results in $w('#textcount12)
$w('#textcount24)
Â
" The method you mentioned is a query by today's date (only)"
You need to read up on how Unix/JS does date and time.
Any date variable by definition has time included (whether you specify it or not) and at the core represents milliseconds since 01/01/1970. You can then take this moment in time and display it in whatever form you need.
Â
Once you understand that, you'll understand what this simple code does.
You set the boundaries as needed (moments in time), then make your queries accordingly.
Â
I'm assuming you are looking back 12 hours and 24 hours and you want just a count of orders in these time frames (last 12 hours, between 12 and 24, more than 24 hours) based on their status (open, closed).
Â
Use the code above to establish the 12 and 24 hour boundaries.
Â
Then use between queries with a simple count:
A query for all orders that have been placed in the last 12 hours and are still "open"
A query for all orders that have been placed between the last 12 hours to 24 hours and are "closed"
I can't write your code for you, but these examples should help you get started on your quest.
Â
Â
Â
Â
Thanks for the help me
Â
But the result is 0
Can you explain why
Â
Â
Â
Â
Â
Â
Â
Take a look at wix-dataset
Â
Place all your code inside the dataset ".onReady()" statement.
You want your code to execute once the database connection is loaded and operating - otherwise you get "0" or random results.
Â
Also, add a few console.log statements to verify that the date/time variables are the way you intend them to be - if you don't know what that is, google "browser console."
To open the developer console window on Chrome, use the keyboard shortcut Ctrl Shift J (on Windows) or Ctrl Option J (on Mac). Alternatively, you can use the Chrome menu in the browser window, select the option "More Tools," and then select "Developer Tools."
Â
Â
Â
Â
I'm really sorry ...... brainstorrrm
The code is working properly
But it seems that the problem I have is the date format
Is there a solution to this problem?
Â
Â
Â
Â
Â
Â
Not sure where you're hanging.
If you placed the query inside the dataset.onReady you should be getting results with this code and the dates you're showing in the collection.
Â
Another thing to consider to do this correctly - you need to ensure that the query time is compatible with the collection time as far as timezone is concerned. To prevent timezone problems, I do all my date/time collections, queries and calculations in UTC time, with:
JavaScript setUTCDate() Method
JavaScript setUTCFullYear() Method
JavaScript setUTCHours() Method
Â
Looking at your console.log and collection orderdate, it seems that this should not be a problem for you.
Â
Â
Wow I found the problem
The old history must be put first and then the new history
Between (Old History) and (New History)
The code is working properly
Really thank brainstorrrm
Â
Â
Â
Â
Oh, but of course.
Glad you figured it out.