Json - Date not showing in correct format - javascript

When i fetch date from the db it is in following format :-
{5/13/2002 12:00:00 AM}
When i pass this as Json, i bind it to textbox & it shows me like this :-
/Date(1021228200000)/
How to display date in any correct format?
Extending this question I want to bind the date to html5 datepicker, How to do this?

var jsonDate = "/Date(1021228200000)/";
var date = new Date(parseInt(jsonDate.substr(6)));
The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.
jQuery dateFormat is a separate plugin. You need to load that explicitly.
Reference

You can use like below to display datetime and bind date to html5 datepicker.
<input class='selector' id='datepicker'/>
$( ".selector" ).datepicker({ dateFormat: 'yy-mm-dd' }).val();

Related

Jquery Datepicker Set Value From String

I am getting a string date from cookie which returns like this format 2017-11-16. My datepicker dateformat is D, M d, yy e.g Thu, Apr 27, 2017.
if I just set it as $("#m_checkin").datepicker().datepicker("setDate", "2017-11-16"); datepicker functions stop working and it just set 2017-11-16 as an input value. How can I format my date to my datepicker dateformat?
https://jsfiddle.net/n6dokkty/
You need to parse date before set date to particular format.
You can see here: https://jsfiddle.net/r1nm5h7k/
$( "#datepicker" ).datepicker({dateFormat: "DD, d MM, yy"});
$( "#datepicker" ).datepicker("setDate", $.datepicker.parseDate( "yy-mm-dd", "2017-11-16" ));
ref : https://api.jqueryui.com/datepicker/#utility-formatDate
You could look into momentjs, which is a javascript date/time library. With that, you can easily convert between dates of different format.
string newDate = moment(currentDate, currentFormatString).format(newFormatString)
You have to setDate with Date object instead of string (to avoid date format issues like you face currently).
So you have to change your code like below:
$("#m_checkin").datepicker().datepicker("setDate", new Date(2017,11,16));

Jquery datepicker change dateFormat to unix timestamp

I'm using the Jquery datepicker to add the selected dates to an SQL database in time format using dateFormat : '#'.
However, this uses js milliseconds and I need it in UNIX timestamp format which is in seconds. So basically I get an extra 3 zeros on the end.
I know I can /1000 to get the correct integer, but how do I do that in the js before it is entered into the db?
This is what I have so far:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('.MyDate').datepicker({
dateFormat : '#'
});
});
</script>
try the below, it has temp value for your SQL DB.
var tempSQLvalue;
jQuery(document).ready(function() {
jQuery('.MyDate').datepicker({
dateFormat : '#',
onSelect: function(date) {
tempSQLvalue = date.slice(0,date.length-3);
console.log(tempSQLvalue);
}
});
})
Posting this answer for anyone else who has the same issue. Instead of using timestamps, I stuck to standard date formats. However, you need to check the different date formats between jquery and php. For example, the date 03-01-16 in jquery will be 'dd-mm-yy' while in php, the same date format would be 'd-m-Y'.

Get local datetime format In Jquery

I want to get local machine datetime format in Jquery?
means if local date time format is in "MM-dd-yyyy" ,so this format I want to get in jquery.
OR
How can I convert my any datetime in local datetime format in jquery?
You can change date format in jquery datepicker as such:
var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
This Link , Link and Link may be helpful.

How to format date value before setting to a date picker input field?

Using JQuery, I created a simple date text field with dateFormat: "mmddyy". There is no problem in the way it works. But the corresponding database field is of type DATE and stores the value in "YYYY-MM-DD" format. So, when I read the value from database and set it to the date text field, the date format appears to be "YYYY-MM-DD", but not "mmddyy". So, how can we format that database date value in JQuery before setting it to the date field ?
Thanks!
Try Jquery Date format plugin to accomplish it.
http://archive.plugins.jquery.com/project/jquery-dateFormat
If you use jQuery UI, check out this page jqueryui.com/demos/datepicker/#option-altFormat
put this code to your datebox
data-options="formatter:myformatter"
and add the function like below
<script type="text/javascript">
function myformatter(date){
var y = date.getYear();
var m = date.getMonth()+1;
var d = date.getDate();
return (m<10?('0'+m):m)+(d<10?('0'+d):d)+y;
}
</script>

Why does changing the dateFormat of my jQuery datepicker fail?

I know there are loads of questions on here about changing the datepicker dateFormat, but i'm following the instructions exactly and they seem to have no effect. Here is the exact code that was suggested on other answers:
$(document).ready(function() {
$('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
});
Yet when I run it, and test the datepicker value with alert($('#datepicker').datepicker("getDate"));
I get the date back in the undesired format! This is extremely confusing. Am I something wrong? See the described behaviour at http://jsfiddle.net/TmdM8/
edit: even more confusing is that if I read the dateformat using alert($("#datepicker").datepicker('option', 'dateFormat'));
... the returned dateFormat is correct, even though calling getDate does not return the date in the desired format. See http://jsfiddle.net/fWmBe/
the date format setting is for what the format looks like when it's show in the text box the picker is applied to.
have a look at my updated version of your fiddle:
http://jsfiddle.net/TmdM8/1/
as you can see, when you pcick in the text box, and pick a date, it is in the format you have specified.
as far as i know, the getDate function returns a javascript date.
EDIT:
if you wanted to format the output from the javascript date, you'll need to use a custom script, or a library like date.js (i've had great success with this).
http://code.google.com/p/datejs/
with that you could do something like :
$('#datepicker').datepicker("getDate").toString('dd-MM-yyyy');
to get your date in the format you like
I agree that it returns a javascript date. You can use this utility function:
$.datepicker.formatDate( format, date, settings ) - Format a date into a string value with a specified format.
from http://docs.jquery.com/UI/Datepicker/formatDate
What is happening here is getDate is return a javascript date object. You are seeing the date object's toString method.
Try this:
$(document).ready(function() {
$('#datepicker').datepicker({
onSelect: function(dateText, inst) { alert(dateText) }
});
$('#datepicker').datepicker( "option" , 'dateFormat', 'yyyy-mm-dd' )
});
in case someone looking for this answer like me. here is a working code:
<script>
$(function() {
$( "#datepicker" ).datepicker({inline: true, dateFormat:'yy-mm-dd'}); });
</script>

Categories

Resources