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)
Related
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())
I have a SQL lookup a date that feeds into a field, but the date format contains time, I need to convert it to short date (mm/dd/yyyy). The MSSQL outputs this format (m/d/yyyy 12:00:00 AM) notice that the time is always '12:00:00 AM'. How do I remove the time?
$('#q60').change(function () {
var date = $('#q60 input').val(); //looking up the field that contains the date fed from SQL.
date = date.substring(0, date.indexOf(' '));
});
I have tried using split but while it output the correct thing it doesn't actually change the value in the field for some reason. I have also attempted using the .format similar to this post: Format a date string in javascript
But I am stuck!
with date = date.substring(0, date.indexOf(' ')); you're just storing splitted value in to date variable. to change the value of the input field add $('#q60 input').val(date) at the end of your function.
also in JS there's a whole Date object, with it you can format your date as you please. you can find more about it here and here
I have this application where I want to use you date, but the problem is that the date is not working as I expect.
I create a date object like this:
// Get today's date
today: function () {
// Create a new date
var date = new Date();
// Set to midnight
date.setHours(0, 0, 0, 0);
// Return our date
return date;
},
and If I output that date in my view I get yesterdays date at 23:00 hours....
Which looks like this:
2015-07-08T23:00:00.000Z
Does anyone know how I can get the date to be formatted properly?
Update
Just to elaborate a bit, I want to use the date to compare against records in the database. These records have the date applied to them, because the JavaScript is showing the local date time, it is not comparing correctly. Also there is a case where I am saving that date and I don't want it to save the local date.
based on your culture setting you can use the
date.toLocaleDateString()
this will give localized string format back
date.toUTCString();
date.toLocaleString();
date.toLocaleDateString();
date.toDateString();
date.toISOString();
Find your answer here :) And the best option is to use momentjs http://momentjs.com/
So, I ended up creating this function:
// Converts a date to a timeStamp
this.convertToTimeStamp = function (dateTime) {
// Get just the date
var date = dateTime.toDateString();
// Get the timestamp
var timeStamp = Date.parse(date);
// Return our timeStamp
return timeStamp;
};
If my understanding is correct, that should create the same date no matter what timezone / locale you are in.
im using moment js date library to format a date, but on IE
i get a NaN on the output. It works fine on other browsers, like Chrome, FF, etc.
var value = "2015-11";
moment(value).format("YYYY-DD-01 00:00")
> "0NaN-NaN-01 00:00"
I was able to fix it by adding the same pattern on moment constructor like below:
> moment(value,"YYYY-DD-01 00:00").format("YYYY-DD-01 00:00")
"2015-11-01 00:00"
Is it a good practice to add this pattern on the constructor, for all moment objects creation
so it can work also on IE?
The input format should match what you are providing:
var value = "2015-11";
moment(value, "YYYY-MM")
If you want to format it differently for output, that's when you use the .format method.
var value = "2015-11";
var m = moment(value, "YYYY-MM")
var s = m.format("YYYY-MM-DD HH:MM")
Note that you were specifying DD which is the day formatter. But based on the usage, I think you meant MM for month.
In Javascript I am trying to write the validation for a date, where the user has to select a future date and not a past date. My code seems to work only when I use a date from last month (e.g. 26/11/2011). This is my script:
<script type="text/javascript" >
function mydate()
{
var d= new Date ();
var day= d.getDate ();
var mon=d.getMonth ();
var year= d.getFullYear ();
var dateformat= day+"/"+mon+"/"+year ;
var get= document.getElementById("txt").value;
if(get >= dateformat )
{
alert ('yes valid');
}
else
{
alert('Date should greater than to date ');
}
}
</script>
You are comparing the date values as strings. This is a textual comparison not a date-wise comparison so it will pretty much never work except by coincidence. You need to parse the date value the user enters into a Date data type and do a comparison that way.
Whenever possible you should avoid writing date manipulation code yourself and try to leverage a known working solution, e.g. the jQuery UI Datepicker.