2

My problem is how to input date in HTML form in this format 21-01-1999 and not in this format 01-21-1999? When I write this HTML code

<input name = "dPregled" id="dat" type="date" required="required" />
  </p>

it gives me mm-dd-yyyy format for input. Also is there a way to automatically take today's date in a form?

I have been researching for an answer everywhere but I can not find it. Thank u so much.

AmraAz
  • 61
  • 1
  • 1
  • 5
  • The format and display of the date on the input side is determined by the client (browser). Most browsers show the input field differently. And most known browsers will use the the locale of the user to determine the input. E.g. I am Dutch and my browser is in Dutch, so the date shows as "dd-mm-yyyy". *Could you add what locale your client is using and what client (browser) you are using?* – berkes Aug 27 '16 at 11:49
  • The date format is determinate by the OS. Like in windows you need to change the OS date format. – Marian Sep 01 '19 at 07:08
  • You are asking two questions here, the protocol is to ask single questions. – Rohit Gupta Sep 27 '22 at 20:30

4 Answers4

1

A quick Google search gives me loads of answers to your question.

From https://developers.google.com/web/updates/2012/08/Quick-FAQs-on-input-type-date-in-Google-Chrome?hl=en (so this applies to Chrome)

Web authors have no way to change the date format because there currently is no standards to specify the format.

For the rest, according to Is there any way to change input type="date" format? and How to set date format in HTML date input tag?, there is currently no way to change the date format. It is all determined by your browser/OS, and because there is no specification yet for how to change the date format, you currently cannot change the format.

However

The Stack Overflow posts mentioned above are quite old, but one of the more recent answers (currently the second one on Is there any way to change input type="date" format?) does provide an answer on how to edit the format, although it requires some playing around with what I'd call somewhat advanced stuff. You can do it with HTML5 and the shadow DOM, which enables you to more or less create your own HTML elements. Older browsers / browser versions don't usually support it too well, though, but you could dig into it a bit and see if it works for you.

Community
  • 1
  • 1
minitauros
  • 1,920
  • 18
  • 20
  • I have found that answer myself but thought that someone more experienced then me can know better. But thank u very much for your answer. – AmraAz Aug 27 '16 at 11:51
  • I've updated my post as I found something that might help you, although it's not something I'd implement myself as to me it doesn't feel stable enough. It's fun to check out and play around with though, so if you feel like it you could learn a bit there :) – minitauros Aug 27 '16 at 11:53
  • Thanks a lot, I will try it! – AmraAz Aug 27 '16 at 11:58
1

Regard to this question:

Also is there a way to automatically take today's date in a form?

Here what I'm gonna do if I need the current date:

//assign that variable to your date field
//today date. 
if(empty ($_POST['date_today'])){
  $_POST['date_today'] = date("Y-m-d");
}
//if u need other date + or - number day to count that date(here is past 5 days)
if(empty ($_POST['date_from'])){
  $_POST['date_from'] = date("Y-m-d", strtotime("-5 day"));
}

in your html (for today) should be like this:

<input type="date" name="date_today" value="<?=$date_today?>" >

don't forget to declare that variable (eg. today date)

$date_today = $_POST['date_today'];
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Qaz
  • 15
  • 4
0
  1. go with Jquery date picker its easy and it can modify date in any format
  2. use Input type text instead so its more convenient to use predefined values
  3. or use this code, and edit as per requirement

basically this code will help you check out

<!-----user will type date in dd-mm-yyyy formate but as he leaves textbox date will be converted to mm-dd-yyyy--->

<!--run it on fiddle : https://jsfiddle.net/rokrd/jp1swqru/9/ --->

<input type="text"  name = "dPregled" id="dat" onchange="dateconverter()" required="required" />



<script>
function dateconverter(){

/*const monthNames = ["Jan","Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];*/


var testDate = document.getElementById("dat").value;


dteSplit = testDate.split("-");
yr = dteSplit[2]; //special yr format, take last 2 digits
month = dteSplit[1];
month = parseInt(month);
//month = monthNames[month];
//remove comment to get output in dd/M/yyyy

day = dteSplit[0];


alert ("converted to mm/dd/yyyy > "+month+"-"+day+"-"+yr);

document.getElementById("dat").value = month+"-"+day+"-"+yr;
}

</script>

Shubham Dange
  • 173
  • 13
0

with firefox, I have the same issue.
I found this link :
https://support.mozilla.org/en-US/questions/1309229
use about:config to set firefox params
and switch intl.regional_prefs.use_os_locales to true and it works fine.

bcag2
  • 1,988
  • 1
  • 17
  • 31