Setting value from a jquery var to a bean - javascript

This is the jquery code
$("#datepicker1").datepicker({
onSelect: function(){
var dateObject = $(this).datepicker('getDate');
}});
And now I want the date from the var dateObject to be set to a bean which is setFromDate(Date date) that takes a Date object as a parameter. How can this be done?

Depends how do u get data from request. Struts can do it using interceptor param.
if u do it manually in servlet than use SimpleDateForm to parse date
//should have same format used in datepicker
DateFormat format = new SimpleDateFormat('mm/dd/yyy');
// i assume that request param name dateObject
Date date = format.parse(request.getParameter('dateObject')
bean.setFromDate(date);
it is possible to assign date format in datepicker as well
http://jqueryui.com/datepicker/#date-formats

Related

How to get date from js in correct format? Month and days get reversed

I am calling an ajax for getting some values for editing data.
As a part of my object, I am sending the date field.
My problem is that when I receive the date value in the controller, date format is wrong - my dates and months are reversed. And because of that I can't compare them where I need to.
But my months and days are reversed. For an example , instead of 3rd October, it returnes 10th of March.
How to fix this?
I am sending the date field from js in a object like this:
ExamsDataU = {
classId: classIdValue,
date: dateValue
};
And in my controller I tried:
DateTime dateToCheck = Convert.ToDateTime(dto.Date);
The first thing you should know is date parsing with Convert.ToDateTime() depends to the current culture used in server (you may check it using CultureInfo.CurrentCulture property). You can try one of these methods to parse JS date format properly inside controller action method:
1) Using DateTime.ParseExact()/DateTime.TryParseExact() with custom format
On this way it is necessary to specify date format before parsing date:
// specify custom format
string dateFormat = "dd-MM-yyyy";
DateTime dateToCheck = DateTime.ParseExact(dto.Date, dateFormat, CultureInfo.InvariantCulture);
2) Using DateTime.ParseExact()/DateTime.TryParseExact() with ISO 8601 format
Use date: dateValue.toISOString(); to convert JS date into ISO 8601 format and then convert it:
// specify ISO format
string dateFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";
DateTime dateToCheck = DateTime.ParseExact(dto.Date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
This way is better than the former because no need to write additional date representation code in client-side, also you can adjust date representation to local time if necessary.
Notes:
a) For specified culture, you can try CultureInfo.GetCultureInfo():
var culture = CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name);
DateTime dateToCheck = DateTime.ParseExact(dto.Date, dateFormat, culture);
b) You can use if condition to check if the date string is valid when using DateTime.TryParseExact():
DateTime dateToCheck;
if (DateTime.TryParseExact(dto.Date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateToCheck))
{
// do something
}
Try this
var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yy');
or
var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[1] + '-' + dateAr[2] + '-' + dateAr[0].slice(-2);
console.log(newDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

How to format a string like `2018911` into a Date?

From backend, the date format is like above, and I want to format the date using formatter into a real date. I got a datepicker in my detailpage and the datepicker wants a real date, to show it up.
So I tried a bit, but I can't get it to work. So maybe someone can help me or guide how to do it? I know I can format the date in the backend but I need it that way like above as a string.
If you are using sap.m.DatePicker here an example:
<DatePicker
id="DP2"
value="2014-03-26" valueFormat="yyyy-MM-dd" displayFormat="long"
change="handleChange"
class="sapUiSmallMarginBottom"/>
there's the valueFormat and displayFormat attribute to shape date format as you want.
valueFormat is the date format you want when user click on date and you can grab in oEvent.
displayFormat is the date format you want to show.
Reference SAPUI 5 DatePicker example
Hi you can create a date from teh string you receiving by using below js code
getDate:function(value){
//value is your string from backend "20120515"
if(value){
var dateString = value;
var year = dateString.substring(0,4);
var month = dateString.substring(4,6);
var day = dateString.substring(6,8);
var date = new Date(year, month-1, day);
return date; // Keep in mind the date returned will only be correct if the string is passed in above format
}
}
You can use the above function in formatter.js file and can use in datepicker as below
<DatePicker value="{path:'modelDateProperty', formatter:'.formatter.getDate', }" />
I hope this helps

Json - Date not showing in correct format

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();

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>

GetFullYear() function does not come after setting date from calendar?

I got a calendar which I want to take birthday of user. Its id is "DatePicker"
var tarih = new Date();
tarih =($("#DatePicker").val());
alert(tarih);
When I run this code I get the date from calendar. The problem occurs when I try to get year which is selected at calendar:
var tarih = new Date();
tarih =($("#DatePicker").val());
alert(tarih.getFullYear());
The code above didn't work. I checked date functions at SO but couldn't find this kind of example.
You can request a Date instance directly;
var tarih = $("#datepicker").datepicker("getDate");
alert(tarih.getFullYear());
i don't know much about JQuery but most probably .val() method will return you a string, so its actually not a date object you have to convert it into a date object before using getFullYear method, you can use Date.parse() function to convert a string to date object

Categories

Resources