Skip Navigation

[Resolved] Date with only Year and Month

This thread is resolved. Here is a description of the problem and solution.

Problem:
Can I have a date picker with just month and year, instead of exact day in month and month + year?

Solution:
https://toolset.com/forums/topic/date-with-only-year-and-month/#post-632314

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 3 voices.

Last updated by Beda 6 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#628916

Hi,

I am using the date fields for dates where I only need the month and year or only the year.
The infuriating jQuery datepicker is forcing me to specify a day. Is there any other way to add a date? such as dropdown, for cases where I don't need a specific day to be specified?

Thank you

#629038

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Adrian

If you only need to store the year, can you not use a numeric field instead? (You mentioned that in our other ticket.)

Date fields are stored as UNIX timestamps, accurate to the second, meaning that under-the-hood you cannot have a date of "2015", you would actually store an explicit date and time, e.g. midnight of 1 Jan 2015.

So even if you are not asking your users for a day and month you must supply one.

I can describe how you might craft a solution to use a date field in a CRED form without a datepicker using some custom code, but I'll only do that if you indicate that's how you want to proceed.

#631855

That makes sense and I will use it as such, but I have another case where I am looking at Month - Year.
The reason I would like to stick to dates, is bc the client may change their mind and want days in there too. If I use any other type of field, then whatever posts he would have to date would have to be adjusted, and we're looking at potentially 1000+ :D. Not something I want to go through at that point.

The concept of transforming it into Unix time isn't too scary, I was able to get a field to convert it into unix time based Today + 30 days and pre-fill the field, which seems to work fine. So taking a Month Dropdown and a year input and converting them should be straightforward.

Question is, is there an elegant way to do it? I'd like to avoid using the date field and using jQuery to remove and add my fields.

Thank you,

Adrian

#632314

What I would do if I need only partial dates is create select fields and populate them with the values (there are even some JSON libraries out there for this).

For example, you can use a CRED generic field (if this happens on the front end which I suggest because you have more control over the fields).
That field can be populated with a valid JSON code, a google search returns several such "libraries", or you can craft your own

That is the simples, in my opinion, and the smoothest too.
Later, with a cred_save_data() action and getting the field's values from the $_POST you can perform whatever transformation to timestamp if needed, or save it straight to the database as is, to a persistent Field, for example, a simple single line that later can be displayed and styled as you wish.