Date format for Input Field - javascript

I just created input date field and tried to change the format. but unable to achieve. is this possible to change the format?
Code Snippet:
<form>
<input value="i want to show 09/09/2015" /><br><br/><br/>
<input type="date" id="dt" value="2014-09-09"/>
</form>
http://jsfiddle.net/HudMe/626/
I wanna show dd/mm/yyyy format.

Use the below datepicker.
https://jqueryui.com/datepicker/
Add below line your script.
$('#datepicker').datepicker({
dateFormat: 'dd-mm-yy'
});

<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
You can change formate of dat by:
$(function() {
$( "#datepicker" ).datepicker();
$( "#format" ).change(function() {
$( "#datepicker" ).datepicker( "option", "dateFormat", <SPECIFY Date Formate here>);
});
});

Assuming you want to change <input type='date'> format so there is no way possible to change the format in <input type="date">.
But you can use https://jqueryui.com/datepicker/ instead of it. see also http://api.jqueryui.com/datepicker/.
After you can change you date format like below :
$.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) );

In Google Chrome it works fine !
For firefox
add small js
(function(){
var target = document.getElementById("dt");
var val = target.getAttribute ('value');
val = val.split("-").reverse().join('-');
target.setAttribute('value',val);
})();
to your fiddle

Related

how to disable all future dates in input type=DatePicker using scripting [duplicate]

This question already has answers here:
jQuery DatePicker with today as maxDate
(5 answers)
Closed 3 years ago.
I'm developing application in iWorkflow platform. It is an object oriented platform. I have DatePicker control for Field named Select Date :.
I want to disable the future dates using javascript for type=DatePicker. But my codes isn't working.
I tried this by adding maxDate:0. But I think $("#ctrl_DatePicker_e0leq").DatePicker works for Input type=Text not DatePicker.
$("#ctrl_DatePicker_e0leq").DatePicker({
maxDate: 0
});
I'm expecting correct script to disable dates greater than the current date.
You can use this approach:
$(document).ready(function () {
var today = new Date();
$('.datepicker').datepicker({
format: 'mm-dd-yyyy',
autoclose:true,
endDate: "today",
maxDate: today
}).on('changeDate', function (ev) {
$(this).datepicker('hide');
});
$('.datepicker').keyup(function () {
if (this.value.match(/[^0-9]/g)) {
this.value = this.value.replace(/[^0-9^-]/g, '');
}
});
});
And:
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Select Date: <input type="text" class="datepicker"></p>
For more Details you can use this link:
$(document).ready(function() {
$("#datepicker").datepicker({
minDate: 0
});
});
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" rel="Stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script language="javascript">
$(document).ready(function() {
$("#datepicker").datepicker({
minDate: 0
});
});
</script>
</head>
<body>
Date :
<div id="datepicker"></div>
</body>
</html>

Text box appear underneath a JQuery DatePicker

I have a datepicker on my website that converts the selected date to a special date and displays it in a read only text box underneath. The thing is, is I want to use the built in feature in the datepicker where I can have it open and close when clicking a link or button.
Just the link opening the datepicker:
<input id="hiddenDate" type="hidden" />
Select Date
$(function() {
$('#hiddenDate').datepicker({
changeYear: 'true',
changeMonth: 'true',
startDate: '07/16/1989',
firstDay: 1
});
$('#pickDate').click(function (e) {
$('#hiddenDate').datepicker("show");
e.preventDefault();
});
});
JFiddle of above
Here is my personal code that I have on an independent page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.25">
<meta name="description" content="">
<meta name="author" content="">
<title>Calendar Converter</title>
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$( function(){
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<div id="calendar"></div>
<p><font font face="Verdana">Output: </font> <input class="target" type="text" id="datepicker2" size="19" readonly></p>
</div>
<script>
$( document ).ready(function() {
$("#hidden_calendar").hide();
});
$( ".target" ).change(function() {
});
$('#calendar').datepicker({
minDate: '2017-02-26', //Base Date
dateFormat: 'yy-m-d',
inline: true,
onSelect: function(dateText, inst) {
var date2 = $(this).datepicker('getDate');
var date1 = new Date("November 10, 2010 00:00:00");
var oneDay = 24*60*60*1000;
var start = 100;
var diff = Math.floor(( date2 - date1 ) / 86400000);
var output = start + Math.floor(diff/7);
$('#datepicker2').val(output);
}
});
$("#hide_calendar").click(function(){
$("#hidden_calendar").toggle();
});
</script>
</body>
</html>
Here is a JFiddle for the above code
As you can see, I have all the components I want, but I just don't how make it so when I click the link the first JFiddle, the textbox in the second JFiddle appears under the datepicker. I hope I am being clear about what I am having trouble with, if need be I can provide an image of what I am trying to do.
Some things I have tried are:
Using a bootstrap modal or popover with both the datepicker and textbox in it, neither worked how I wanted it to.
I have also tried modifying the datepicker but I just messed it up and had to revert it.
Any suggestions? I feel like what I am trying to do is trivial and it is bugging me I can't figure it out.
Not a straight forward solution to your problem, but you could use jQuery ui dialog to open a modal with datepicker in it.
$(function(){
$("#dialog").dialog({
autoOpen: false,
minWidth: 350,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "fadeOut",
duration: 1000
}
});
$("#opener").on( "click", function(e) {
e.preventDefault();
$( "#dialog" ).dialog( "open" );
});
$('#calendar').datepicker({
minDate: '2017-02-26', //Base Date
dateFormat: 'yy-m-d',
inline: true,
onSelect: function(dateText, inst) {
var date2 = $(this).datepicker('getDate');
var date1 = new Date("November 10, 2010 00:00:00");
var oneDay = 24*60*60*1000;
var start = 100;
var diff = Math.floor(( date2 - date1 ) / 86400000);
var output = start + Math.floor(diff/7);
$('#datepicker2').val(output);
}
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JQuery Datepicker</title>
</head>
<body>
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="dialog" title="Basic dialog">
<div id="calendar"></div>
<lable>Output:</lable>
<input class="target" type="text" id="datepicker2" size="19" readonly>
</div>
Select Date
</body>
</html>
As you already using jQuery ui, you could achieve this without using any additional library or plugins.

jquery datepicker has different format on different browsers

im using jQuery datepicker in my php code specifically for the browsers like IE and Firefox. The default date format of datepicker is mm/dd/yyyy, however for chrome, since the date picker is already available, it shows dd/mm/yyyy. Whatever the display format is, i only want to send back to the server 'yyyy-mm-dd' or as in php date('Y-m-d)
How can i display the date from datepicker as dd/mm/yyyy and send to database as yyy-mm-dd? In doing so, i don't want to affect Chrome which is already working properly. Thanks for any advice.
You can use the dateFormat option:
$( "#datepicker" ).datepicker(
"option", "dateFormat", "yy-mm-dd"
);
Set your datepicker format try this :-
$("#datepicker").datepicker("option", "dateFormat", 'yy-mm-dd' );
More details
You have to specify dateFormat other wise its dependent to browser.
$(function() {
$( "#datepicker" ).datepicker();
$( "#format" ).change(function() {
$( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Format date</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<p>Date: <input type="text" id="datepicker" size="30"></p>
<p>Format options:<br>
<select id="format">
<option value="mm/dd/yy">Default - mm/dd/yy</option>
<option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
<option value="d M, y">Short - d M, y</option>
<option value="d MM, y">Medium - d MM, y</option>
<option value="DD, d MM, yy">Full - DD, d MM, yy</option>
<option value="&apos;day&apos; d &apos;of&apos; MM &apos;in the year&apos; yy">With text - 'day' d 'of' MM 'in the year' yy</option>
</select>
</p>
</body>
</html>
Reference: jqueryui datepicker
Thanks to Rahautos. I did this:
<script type="text/javascript">
if (datefield.type!="date"){ jQuery(function($){
$('#datepicker').datepicker();
$("#datepicker").datepicker("option", "dateFormat", 'yy-mm-dd' );
})
}
</script>

why is not working datepicker code

<?php include('include_navigation.html');?>
.all code is right and work but when
i have use this code
in my page then date-picker is not working . why it's happen plz tell me Where the
datepicker should appear? and also
when i have remove this code date-picker code is working and i have not getting what
happen so plz tell me what is wrong in here.....
date-picker code here......
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness
/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
.datepicker{
}
</style>
<script>
$(function() {
$( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
});
</script>
html code
<tr>
<td>Admission Date</td>
<td height="48">:</td>
<td height="48"><label><input type="text" name="AD_DT" id="AD_DT"
class="datepicker" style="width:80px;" /></label></td>
</tr>
`
Try this code
$(document).ready( function() {
$( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
});

Custom Text in jQuery Datepicker

I have to display date in input box after picking from datepicker using jQuery.
I have used following code:
$(function () {
$("#datepicker").datepicker({
minDate: -0,
maxDate: "+1M +2D",
showOn: "button",
buttonImage: "calendar.png",
buttonImageOnly: true,
dateFormat: 'D dd MM' });
$.datepicker.formatDate('yy-mm-dd');
});
And I am able to get date in this format: "Fri 09 August".
However my requirement is: Fri 09 August (Today)
Here's a JS Fiddle to work upon: http://jsfiddle.net/4cgPe/
Any idea how I could achieve this?
I couldn't find much about adding custom text to jQuery, so I came up with my own JavaScript.
I built upon your DatePicker code.
(What I understood was, if the selected date is of today, then add " (Today)", OR, add " (Tomorrow)" if selected date is of tomorrow OR, add nothing for other dates.
<html><head><link type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js" ></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" ></script>
<script type="text/javascript">
$(function () {
$("#datepicker").datepicker({
minDate: -0,
maxDate: "+1M +2D",
showOn: "button",
buttonImage: "calendar.png",
buttonImageOnly: true,
dateFormat: 'D dd MM yy' });
$.datepicker.formatDate('yy-mm-dd');
});
function updateDate(){
var inputDate=document.getElementById('datepicker').value;
var selectedDate=new Date(inputDate);
var today=new Date();
var tomorrow=new Date();
tomorrow.setDate(today.getDate()+1);
selectedDate.setHours(0,0,0,0);
today.setHours(0,0,0,0);
tomorrow.setHours(0,0,0,0);
var DaysFull=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var DaysShort=["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var MonthsFull=["January","February","March","April","May","June","July","August","September","October","November","December","January"];
var MonthsShort=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan"];
var finalDate=DaysShort[selectedDate.getDay()]+" "+selectedDate.getDate()+" "+MonthsFull[selectedDate.getMonth()];
if(selectedDate.getTime() == today.getTime()){
document.getElementById('datepicker').value=finalDate+" (Today)";
}
else if(selectedDate.getTime() == tomorrow.getTime()){document.getElementById('datepicker').value=finalDate+" (Tomorrow)";}
else {document.getElementById('datepicker').value=finalDate;}
}
</script>
</head>
<body>
<form><input type="text" id="datepicker" onchange="javascript:updateDate()" /></form>
</body></html>
EDIT:
Additional Functionality: There may be a requirement to use Full Days/Months name or Short Days/Months, so you can use the array accordingly.
DaysFull: Sunday, Monday, etc.
DaysHalf: Sun, Mon, etc.
MonthsFull: January, February, etc.
MonthsHalf: Jan, Feb, etc.
Hope this helps. Let me know if this is not enough. :)
Full Source Code as below for different types of format :
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Format date</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
$( "#format" ).change(function() {
$( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" size="30" /></p>
<p>Format options:<br />
<select id="format">
<option value="mm/dd/yy">Default - mm/dd/yy</option>
<option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
<option value="d M, y">Short - d M, y</option>
<option value="d MM, y">Medium - d MM, y</option>
<option value="DD, d MM, yy">Full - DD, d MM, yy</option>
<option value="'day' d 'of' MM 'in the year' yy">With text - 'day' d 'of' MM 'in the year' yy</option>
</select>
</p>
</body>
</html>
More about format refer This link

Categories

Resources