Working with dates and time in WIX Code?

There is a lot of people that has problems with dates and time in WIX Code. I have worked hard to understand this and here I share my thoghts.

There is something called servertime in my world, that is the date / time together with the servers timezone. So when you get date / time from server maybe you will get the servers timezone.

There is something called local date / time and that is your users browsers date / time timezone.

So when you work with date / time you might want to check JavaScript Date getTimezoneOffset() Method and think about always storing date / time in UTC and always convert to and from UTC using difference between UTC and your users local time settings.

This is a probleem because usually javascript will execute in the users browser with local settings and sometimes javascript functions will execute on the server.

Have anyone the same thoughts? So in what timezone is the servers placed?

Hi Andreas, I am working more or less on the same thing. What I ran into, is the following: the userĀ“s local time settings are also unreliable. It happened to me. I have 2 clocks running, my first in European time and my second in South-American (where I am now) time. Most software checks the userĀ“s ā€œtimeā€, thus time 1. ThatĀ“s why all my appointments go wrong, they are 4 or 5 hours off.
It would be better to check the IP-block locale to guess what time it is in the userĀ“s location. And always store as UTC and display behind it a local time based on IP time zone. International travellers would have less problems this way.

Hi guys.
Honestly, unless you know javascript wellā€¦ we are going to struggle. I am learning javascript hereā€¦ JavaScript Tutorial

I have a quoteDate field in the collection.
I want to add a NUMBER of days to that date to show ad deadlineDate.
25 March 2018 + XX days = ?

How do I add numbers to a date value in the data.js file?

Hi Wayne, Iā€™m working on something similar ā€“ setting an ā€œexpirationā€ date 1 year from when a record was created in the database
createdDate + 365 days = ā€¦

Did you every get the code working for this?

Thanks!

Hi Annie.

No. Iā€™m still struggling my backside off. Isnā€™t there a webinar course for Six code?

Try this

var date = new Date(tt);
    var newdate = new Date(date);

    newdate.setDate(newdate.getDate() + 3); // Adding three days
    
    var dd = newdate.getDate();
    var mm = newdate.getMonth() + 1;
    var y = newdate.getFullYear();

    var someFormattedDate = mm + '/' + dd + '/' + y;
1 Like

Hi Andreas,
Would you please explain what you are doing.
I get red dots errors saying ā€œtt is undefinedā€.
Here is my code
The field I need to add to = quoteDate

var quoteDate = new Date(tt); 
var newdate = new Date(quoteDate); 

newdate.setDate(newdate.getDate() + 3); // Adding three days 

var dd = newdate.getDate(); 
var mm = newdate.getMonth() + 1; 
var y = newdate.getFullYear(); 
var someFormattedDate = dd + '/' + mm + '/' + y;

Andreas, thank you, thank you, this worked!

Nowā€¦ I have another date formatting question. I have 2 dates listed, 1 directly from the database (which I previously formatted using Liranā€™s code from this post ) then a second date, which is calculated from a date within my database (calculated using code from this article and reformatted using your code above)

So all of the code works correctly (see below), but my problem is that now both dates are formatted differently from one another:


Ideally, Iā€™d like to see both as ā€œApr 18 2018ā€ (or something similar), but at this point Iā€™ll settle for just having them the same format as each other

Hereā€™s the page code

And hereā€™s the data.js from the backend Iā€™m working with:

Any thoughts?

Thanks in advance :slight_smile:

Make a Function that will take a date and return it in the format you need and run all date outputs through that function.

tt needs to be a date. Let tt = New Date();

Andreas can you help me out with the code and/or where it should go to do that? Iā€™m surprised I got this farā€¦ Iā€™m not entirely sure how to move forward

  1. Would the Function be on the page code or the data.js?
  2. What would that code look like
  3. How do I format the date correctly ā€“ I see the split/splice method a lot and I read Wixā€™s articles but not sure I understand how those work on the dates, and with your method, Iā€™m not sure how to change the XX/XX/XXXX format to Mon XX 201X

Thanks again!

NVM Andreasā€¦ got it working! Will post some code & notes shortly. Thanks so so much for your help!

1 Like

Wayne (and anyone else who needs it) ā€¦ hereā€™s what I ended up with (it works!!)

  1. Show a date from my database
  2. Calculate a date X days after the original start date, and show that on the page as well
  3. Format both dates to read as Month Date, Year (ā€œApril 20, 2018ā€)

Note: you can format the date any way you need by making a few changes to the line:
// formats date to Month Date, Year
var expDate = longMonth + " " + day + ", " + year;

Hereā€™s the first part of the CODE ā†’

(This goes into data.js in the backend)
export function Members_afterQuery(item) {
item.expDate = setMemberExpDate(item._createdDate);

return item;

function setMemberExpDate(_createdDate) {
var originalDate = new Date(_createdDate);
var date = new Date(originalDate);

// set expiration 1 year 
date.setDate(date.getDate() + 364); 

const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 

const day = date.getDate().toString(); 
const longMonth = monthNames[date.getMonth()]; 
const year = date.getFullYear().toString(); 
	
var expDate = longMonth + " " + day  + ", " + year; 

return expDate.toString(); 

}
}

THEN

(This goes into the page code ā€“ in this case, Iā€™m using static text fields on a dynamic page)
$w.onReady(function () {
$w(ā€œ#membersDatasetā€).onReady(() => {
populateCalculatedFields();
reformatJoinDate();
});
});

function populateCalculatedFields() {
const currentItem = $w(ā€œ#membersDatasetā€).getCurrentItem();
$w(ā€œ#expDateā€).text = currentItem.expDate;
}

function reformatJoinDate() {
let originalDate = $w(ā€œ#joinDateā€).text;
let date = new Date(originalDate);

const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 

let day = date.getDate(); 
let longMonth = monthNames[date.getMonth()]; 
let year = date.getFullYear(); 
	
$w("#joinDate").text =  longMonth + " " + day  + ", " + year; 

}

A few notesā€¦
Iā€™m sure the function reformatJoinDate can be simplified, as this is mostly a copy of the code in data.js ā€“ but I kept getting stuck there, so this works

Also, if you are doing this in a repeater youā€™ll need to add a line after the onReady
$w(" #repeater1 ").forEachItem( ($w) => {

So Wayne, youā€™ll need to make a few changes to make this work for you:

  1. My expDate becomes your quoteDate
  2. Change the ā€œMembersā€ in Members_afterQuery(item) to the name of your database
  3. If your original date doesnā€™t come from the _createdDate, change that as well
  4. Youā€™ll need to make changes to the page code as well to connect to your page elements

Good luck, let me know how it goes!

Hi again everyoneā€¦ I think I jumped the gun ā€“ my code ā€œworksā€ but itā€™s only displaying the most recent database entry in the live version (in preview it works correctly and displays each userā€™s info on an individual page)

I know Iā€™ve seen this while reading thru the forum, but canā€™t find a good answer for how to fix my code. Can anyone help?

Andreas maybe? :slight_smile:

Thanks!!

Hey Annie
Are you sure you have the correct permissions setup on the Data Collection? This sometimes mess things and errors up a bit. And also check that you have the correct records in both live and sandbox so you know that the code itself have the same records to work with.

Then come back to me.

#datacollection #sandbox

Hey Andreas

I checked thru everything againā€¦ I donā€™t think there should be an issue with my permissions:


And the sandbox and live versions have all the same fields ā€¦ plus the only ones Iā€™m referring to in the code itself are _createdDate (which is automatic from Wix), plus expDate (checked this is all right) which is a calculation and works properly in the code in both preview and liveā€¦ so I think my only issue is the getCurrentUser and related items, which should be whatā€™s directing the dynamic page to each individual user

Right now, when I check on live, itā€™s actually directing me to a Members page with a userID thatā€™s not associated with any member in my database ā€“ which is also confusing

Live site: grounddelivery.org
My member profile should go here: grounddelivery
But instead, if I follow the login process and click the correct buttons ā€“ which all use the following code ā€“ I end up here every time now: grounddelivery

Button code to get to MY MEMBER PROFILE:
export function accountButton_click() {
wixLocation.to(/Members/${wixUsers.currentUser.id});
}

Not sure whatā€™s going on, so any advice you have would be great! Not sure if I need to verify the user using their email also/instead of the userID ā€¦

Let me know what you think! Thanks again!

So, Iā€™m testing everything I can think of, and something else strange seems to be happeningā€¦
My user ID as a registered member of the site is 92f412b6-34ce-4df1-b0fc-6029f7de3e4a
and if I type this into the URL, it works and I see my profileā€¦ but when I navigate there using my menu & buttons, I always end up at a 404 Error page now with a userID in the URL that doesnā€™t match any in the database
So I think the issue is definitely with the code on the Members profile page, because on that page I have a button that links to the Members update (ID) dynamic page, and that one works correctly and takes me from my profile to my profileā€™s update page ā€“ so something with getting the current userā€™s info in my code ā€“ which is necessary to calculating my expiration date and reformatting the dates ā€“ seems to be my issue ā€¦ I think

Any suggestions Andreas?

Hi all, Iā€™m STILL stuck on this ā€“ any suggestions on how to fix? PLEASE?

I have the following question.
When I new a date and insert into one of the field in my collection.
The new date is a number. ACTUALLY, when I mouse over the field in the collection.
There is a blue word which can automatically convert the number to UTC format.
Is there anything I can insert the data in UTC format without this manual procedure.
Hope someone can help.

var now = new Date();
var utc_timestamp = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() ,
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());

//$w(ā€˜#text30ā€™).text = timeDiff.toLocaleString();

let Jinsert = {
ā€œnameā€: (buttonname),
ā€œstartā€: utc_timestamp
}
wixData.insert(ā€œAttendDBā€, Jinsert)
.then((results) => {
// item = results; //see item below
})
. catch ((err) => {
let errorMsg = err;
});