- SOLVED - Formatting date back to database?

Hey Wix team,

I have a slight issue that when an element is used to setFieldValue to the database the date stays formatted i.e. only the date is stored and no time, this shows a fault in the database. The field type is date and time, below is the code I’m using to save to the database for the date

$w('#courseAvailabilityDataset').setFieldValue('date', getDate)

Would someone be able to help me with the code to change the format back to the original database format?

Many thanks!

Anyone able to help with this one?

Hi Stephen,

What is the scenario that you need both date and time stored in the “getDate” field? Sometimes another approach could work as well.

Here are a few possible solutions:

1. Use the Javascript Date( ) function to format the date and time prior to calling “setFieldValue” (hint: “10” & “33” below at the end represent the Hour and Minute respectively).

var getDate = new Date(2018, 11, 24, 10, 33); // reference:> https://www.w3schools.com/js/js_dates.asp

$w('#courseAvailabilityDataset').setFieldValue('date', getDate)

You would then need to replace the date & time values with your user input controls’ values to something like this:

// ADD a "DatePicker" control to your page
var YYYY = $w('#datePicker1').value.getFullYear();
var M = $w('#datePicker1').value.getMonth();
var D = $w('#datePicker1').value.getDate();
// ADD a "TimePicker" control to your page
var HH = $w('#timePicker1').value.getHours();
var MM = $w('#timePicker1').value.getMinutes();

var getDate = new Date(YYYY, M, D, HH, MM); // reference:> https://www.w3schools.com/js/js_dates.asp

$w('#courseAvailabilityDataset').setFieldValue('date', getDate)
  1. If you simply need the Date and Time that the row was created, then WIX automatically does that for you. There is a Hidden “Created Date” field in each collection. So you wouldn’t actually need to save anything. See screenshot below:

  1. If you want a really simple way and you need to save the Date and Time for an Event, Meeting or Class, maybe it would be good to create 2 separate fields in your Collection. One for the Event Date. One for the Event Time. That way you could use databinding to the input controls. See example below:


If you do it this way, then you will need to set your EventTime field’s data type to “TIME” not to “DATE TIME”.

Hope this helps or gets you in the right direction.

Let me know if this helps or if you need more information.

Best,
Nick

Hey Nick,

Thanks for the detailed response, I have some photos that might explain better what I’m trying to achieve.
So when someone books a course, the date they see has been formatted to show on the screen, but when they book it saves some data to a database to ‘log’ how many people are booked onto that course. But when it saves to the database it only saves the date and displays a fault shown in the bottom photo.

The code to save to the database is

let getDate = local.getItem('date');

$w('#courseAvailabilityDataset').setFieldValue('date', getDate) 

Thank you!

Hey Stephen -

Ok, got it. Try this.

let getDate = local.getItem('date'); 
let formattedDate = new Date(getDate);  // new line

$w('#courseAvailabilityDataset').setFieldValue('date', formattedDate );  

Let me know if this works.

Best,
Nick

Hey Nick,

Works great! Thank you.

Stephen

@stephenmccall Great, glad it works! :slight_smile:

hey… i want the live date and time from the server itself not from the users machine … how do i do that? pllz help me