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

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

Related

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

What format does .getDay() accept?

Ok, so what I want to do is to retrieve some info from an input element, which is of type date. I then store this in a .json file, and I parse it later on to retrieve the data upon program start. After that, I want to use the date.getDay() function to figure out what day that date falls on.
I have only seen examples using var d = new Date(), and something tells me that the 'format' is different when using new Datethan using document.getElementById("dateInput").value;
Anyone catch my drift?
To sum up, I want to be able to find the day from the values outputted by an input type = "date" element.
Date.getDay() does not accept the default type (String) that gets returned by the elem.value()-call.
You have to pass a Date.
So you can convert any legit string to a date using the "new Date()" constructor.
let dateString = "2018-03-08"
let dateFromString = new Date(dateString) // working
Please have a look at this example code:
let date = new Date(mydate.value)
let day = date.getDay()
console.log(day)
<input id="mydate" type="date" value="2018-03-08">
You could also find some helps in the docs:
Date JavaScript
JavaScript Date Reference
Well, Date.getDay() does not accept any input, it is an "instance method", if you will, of the Date class. This means a Date object must be instantiated before you can call getDay() on it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
I think what you're trying to do is something along these lines:
<input id="js-date" type="date" value="2018-03-08">
let el = document.getElementById('js-date')
let date = new Date(el.value)
alert(date.getDay())

DD/MM/YYYY Date format in Moment.js

How can i change the current date to this format(DD/MM/YYYY) using moment.js?
I have tried below code.
$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");
But it's return 0037-11-24T18:30:00.000Z. Did't help to format current date.
You need to call format() function to get the formatted value
$scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
//or $scope.SearchDate = moment().format("DD/MM/YYYY")
The syntax you have used is used to parse a given string to date object by using the specified formate
You can use this
moment().format("DD/MM/YYYY");
However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.
var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
This worked for me
var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP
moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
This actually worked for me:
moment(mydate).format('L');
for anyone who's using react-moment:
simply use format prop to your needed format:
const now = new Date()
<Moment format="DD/MM/YYYY">{now}</Moment>
A safe way to do this
moment.locale('en-US');
moment().format("L");
"06/23/2021"
moment.locale('fr');
moment().format("L");
"23/06/2021"

Can't Compare dates using jQuery UI's datepicker

Ok, so I am attempting to test if a date is older than today. I am using jQuery UI's Datepicker to parse the date and assign it to a variable:
//Get Date as String
var $strDate = $(".pmt-date").text();
//Parse Date
var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
Then I get today's date and assign it to a variable:
//Get Today's Date
var $strToday $.datepicker.formatDate('mm/dd/yy', new Date());
var $tDate = $.datepicker.parseDate('mm/dd/yy', $strToday);
Now I would like to compare $dtDate with $tDate. This is what I have tried:
if($dtDate > $tDate)
{
alert("Payment Date is Greater");
}
else
{
alert("Today's Date is Greater");
}
When I test this, I ALWAYS get the alert "Today's Date is Greater". I can display my two date variables via an alert, and I see the dates in correct format. So why does this comparison fail to work when the parse is working correctly?
Assuming that the field with class "pmt-date" is the datepicker-controlled <input> element, you need to fetch its value with .val(), not .text().
var $strDate = $(".pmt-date").val();
Your next line of code refers to a variable called "$date", not "$strDate", so:
var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
Once you've got that, you can just directly compare the Date objects:
if ($dtDate < new Date())
There's no need to turn a newly-constructed Date object into a string and then back into a date. I guess you're Date to string and back in order to strip off the time-of-day part of the date, so that's not really a bad way to do it.
In date comparisons, more than means the date comes after, and less than means the date comes before. Older than would imply that the date comes before, and thus you want to use less than
if($dtDate < $tDate)

Setting value from a jquery var to a bean

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

Categories

Resources