jQuery UI dateFormat not changing format - asp.net-mvc

I have a datepicker that I want to apply a custom format to but it does not work.
$(function () {
$('.datepicker').datepicker({
dateFormat: "dd-mm-yy"
});
})
But the custom format is not being applied, it stay with the default "mm/dd/yy"
Any clues what else can impact the fomating? Its an old mvc .net project with EF. Can some of the EF tags impact this??
Any help is greatly appreciated
Thanks!

One of the reasons this can fail is if you're making two calls to datepicker() in the same file, and one is overwriting the results of the other.
HTML
<html>
<body>
<input type="text" class="datepicker">
</body>
</html>
JS
$(function () {
$('.datepicker').datepicker({
dateFormat: "dd-mm-yy"
}); // Initialized once
});
$('.datepicker').datepicker(); // Initialized again without any options

Thanks for the input guys but I found the issue - turns out there was a refrence to another js libary called bootstrap-datepicker.js in the solution that was interfering with the jQuery UI libary. Sorry for the bad question

Full Example is here with your needed format:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Format date</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.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>
<script>
$( function() {
$("#datepicker").datepicker();
$("#format").on( "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="dd-mm-yy">Other - dd-mm-yy</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>

Related

ASP.NET MVC : DateTime is taking format as MM/dd/yyyy but I need in dd/mm/yyyy [duplicate]

This question already has answers here:
jQuery UI DatePicker - Change Date Format
(29 answers)
Closed 2 years ago.
public class Employee
{
[DataType(DataType.Date)]
public DateTime DateOfRegistration { get; set; }
}
My .cshtml:
#Html.EditorFor(model => model.DateOfRegistration,
new { htmlAttributes = new { #class = "form-control", #readonly = "true" } })
jQuery code:
$('input[type=datetime]').datepicker({
dateFormat: "dd/M/yy",
changeMonth: true,
changeYear: true,
});
Please help me - how can I get date in dd/mm/yyyy format?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Format date</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.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>
<script>
$(function() {
$("#datepicker").datepicker();
$("#format").on("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="dd-mm-yy">dd-mm-yy</option>
<option value="dd/mm/yy">dd/mm/yy</option>
</select>
</p>
</body>
</html>
for more help kindly refer Change Date Format of jQuery Datepicker to dd/mm/yy
Change your dateFormat from this: dateFormat: "dd/M/yy",
to this: dateFormat: 'dd/mm/yy'
More info here and here.
Hope this helps

bootstrap date formate getting wrong result

i want date formate in dd/MM/yyyy
using js and css
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
my jquery script
$(function () {
$('#DateOfBirth').datetimepicker({
format: 'dd/mm/yyyy'
});
});
but output is
Tu/00/yyyy
Your format should be like this: "DD/MM/YYYY"
$(function () {
$('#DateOfBirth').datetimepicker({
format: "DD/MM/YYYY"
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<div style="position: relative">
<input type="text" id="DateOfBirth" class="form-control" />
</div>

Is onchnge() function works in jquerymobile 1.4.2

I am using jquerymobile 1.4.2.This the code which i am using in my page
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"> </script>
</head>
<body>
<script>
function myFunction()
{
document.getElementById("myText").disabled=true;
}
</script>
<p>Click the button to disable the text field.</p>
<select onchange="myFunction()"><option>1</option><option>2</option></select>
First Name: <input type="text" id="myText">
</body>
</html>
The above program works fine But if i change that to this code
<script>
function myFunction()
{
document.getElementById("myText").disabled=false;
}
</script>
<p>Click the button to disable the text field.</p>
<select onchange="myFunction()"><option>1</option><option>2</option></select>
First Name: <input type="text" id="myText" disabled="disabled">
Then its not working please help me how to make text field enable using onchange() function
jQM enhances the input and creates a Textinput widget which has its own enable/disable methods (http://api.jquerymobile.com/textinput/#method-disable)
In your example, to enable the input:
function myFunction() {
$("#myText").textinput("enable");
}
Also, you should use unobtrusive javascript and the jQM page functions. e.g. remove the onclick from the markup:
<select id="theSelect" >
<option>1</option>
<option>2</option>
</select>First Name:
<input type="text" id="myText" disabled="disabled" />
Add the handler in the pagecreate jQM event:
function myFunction(selVal) {
if (selVal == '2'){
$("#myText").textinput("enable");
} else {
$("#myText").textinput("disable");
}
}
$(document).on("pagecreate", "#page1", function () {
$("#theSelect").on("change", function(){
var v = $(this).val();
myFunction(v);
});
});
Here is a DEMO

using pageinit() instead of document.ready

I tried many ways of using my app as a jquerymobile but didn't succeed.
I know I have to use pageinit instead of document ready. Can you please show me the right way to do it. When I add it, it just simply not working. Here is code
<head>
<link rel="stylesheet" href="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script>
if (navigator.userAgent.match(/Android/i)) {
window.scrollTo(0, 1);
}
</script>
<script src="https://s3.amazonaws.com/codiqa-cdn/jquery-1.7.2.min.js"></script>
<script src="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
try {
$(function() {});
} catch (error) {
console.error("Your javascript has an error: " + error);
}
</script>
<script>
$(document).ready(function() {
$("form").change(function() {
$.post("test2.php",
$("#myform").serialize());
});
});
</script>
</head>
<body>
<div id="my-page" data-role="page">
<form name="myform" id="myform" method="get">
<label for="DogalGaz">Doğal Gaz</label>
<select name="DogalGaz" id="DogalGaz" data-theme="" data-role="slider">
<option value="">Seçiniz</option>
<option value="off">Off</option>
<option value="on">On</option>
</select>
</form>
</div>
What i did was, just simply send the form data to test2.php file and process. Thank you for any help
The Flip Toggle Switch should have 2 options instead of 3.
Try this:
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script>
if (navigator.userAgent.match(/Android/i)) {
window.scrollTo(0, 1);
}
</script>
<script src="https://s3.amazonaws.com/codiqa-cdn/jquery-1.7.2.min.js"></script>
<script src="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
$(document).on('pageinit', function() {
$("#myform").change(function() { // use #myform instead of form to only serialize one form.
$.post("test2.php",
$("#myform").serialize());
});
});
</script>
</head>
<body>
<div id="my-page" data-role="page">
<!-- include action and method in case javascript handler fails -->
<form name="myform" id="myform" action="test2.php" method="post">
<label for="DogalGaz">Doğal Gaz. Seçiniz: </label>
<select name="DogalGaz" id="DogalGaz" data-theme="" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</form>
</div>
</body>
</html>
This will post to test2.php every time you switch the toggle.

Cannot use click action in jquerymobile

I tried add click function on item, but seems that does not work correctly.
$(document).ready(function(){
$("#vibrateBtn").click(function() {
window.alert('test');
});
});
I tried also disable Ajax loading, with>
$(document).bind("mobileinit", function(){
ajaxEnabled:false;
});
But with same result.
Where can be problem please?
Thanks for any advice.
in jquery mobile document.ready() doesn't work and use .live to bind the click event instead of using the short hard .click().
There are other event like pagebeforecreate, pagecreate. check the jquery demo for the list of event.
$(document).live('pageshow',function(){
$("#vibrateBtn").live('click',function() {
window.alert('test');
});
});
Please try this code
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script language="javascript">
$(function() {
$('#theButton').on('tap click',function(event, ui) {
alert('The Button has been clicked');
});
});
</script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<input type="button" id="theButton" name="theButton" value="The Button">
</div>
</div>
</body>
</html>

Resources