AmraAz
AmraAz

Reputation: 61

How to change date format in html from mm-dd-yyyy to dd-mm-yyyy?

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.

Upvotes: 2

Views: 12691

Answers (4)

bcag2
bcag2

Reputation: 2439

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.

Upvotes: 0

Shubham Dange
Shubham Dange

Reputation: 182

  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>

Upvotes: 0

Qaz
Qaz

Reputation: 15

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'];

Upvotes: 1

minitauros
minitauros

Reputation: 2030

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.

Upvotes: 1

Related Questions