dates with different formats change to 1 dateformat

Hi There

I have a question about dateformats

I am searching for a solution/validation for the next problem. in the formfields I get 2 different dates:
yyyy-mm-dd and dd-mm-yyyy. Is there a javascript to think of. doing the following:

if (date = dd-mm-yyyy) {convert to yyyy-mm-dd}

I hope someone can help me


Ron BaasDirecteurAsked:
Who is Participating?
Chris StanyonCommented:
Hey Ron,

Something I'm unsure of in your code. You have your 2 date fields - afvoer and opdrachtdatum, and these will be sent to the server when you submit the form, so I don't really understand why you have a hidden field. All you seem to do with that hidden field is set it to the value of opdrachtdatum when you submit the form. This would effectively give you the same value in 2 different form fields - $_POST['opdrachtdatum'] and $_POST['neddatum'].

As for the date format, a DatePicker will always submit the value in the yyyy-mm-dd format, regardless of how it's displayed on the screen. As you say, this is perfect for the Database. If you want to change the format of it for your email, you simply create a new DateTime object from the value and then call the format() method when you use it, passing in the format string you want to output. So in your PHP scripts, you would do something like the following:

// create a date time Object
$toDate = new DateTime( $_POST['opdrachtdatum'] ); // 2016-06-15

// format it 
echo $toDate->format( 'm-d-Y' );  // 15-06-2018
echo $toDate->format( 'jS F Y' ); // 15th June 2018

Open in new window

Chris StanyonCommented:
OK. You've tagged this as PHP and Javascript, so not sure what you need. You mention formfields. Are you wanting to handle the form data once it's passed back to your server (i.e. PHP).

If you are talking about client-side, is there are a reason why you have different date formats in your HTML. It would make sense to normalise those
Ron BaasDirecteurAuthor Commented:
Hi Chris

It is a very strange problem, this is the formfield:

  <div class="form-group">

         <input type="date" placeholder="jjjj-mm-dd" name="opdrachtdatum" class="form-control"/>

some dates are   yyyy-mm-dd    and some dates are   dd-mm-yyyy.

I hav also attached de file. I hope you can help me with this strange problem


Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Chris StanyonCommented:
Hey Ron,

You haven't attached any files.

Basically, if your inputs are set to the date type, then the values that are submitted should all be normalised to the standard date format - yyyy-mm-dd - when your form is sent to the server. The inputs may display a different format to the user as this is based on locale, but the actual value submitted should always be in the standard format.

Now if your inputs are just plain text inputs, then the browser will do nothing to sort your dates out and the data sent will be in whatever format the user typed it in.

Would probably need to see your code to help you out further.
Ron BaasDirecteurAuthor Commented:
Hi Chris

Thanks, I'll try it this way
Ron BaasDirecteurAuthor Commented:
Hi Chris or someone else,

I have still one problem with my form.
For the database is the date perfect but for the emailsending I prefer  mm-dd-yyyy.

I hope you can help me with this  the inputfield is:  "opdrachtdatum"  
I have make a hiddenfield with  the name  "neddatum"  and a javascript function dataform.
The "neddatum" value should be dd-mm-yyyy

Regards Ron
Ron BaasDirecteurAuthor Commented:
thanks chris
All Courses

From novice to tech pro — start learning today.