i have textbox when user enter data with MM/dd/yyyy format then i need to parse that data and convert it to date. i used momentjs but my code not working. here is my code snippet.
var otherProp = $('#' + param.otherproperty);
var StartDate = new date(moment(otherProp.val(), 'MM/dd/yyyy'));
specially this line is not working var StartDate = new date(moment(otherProp.val(), 'MM/dd/yyyy')); where i made the mistake ?
thanks
UPDATE
my full code as follows
$.validator.addMethod("isgreater", function (value, element, param) {
var otherProp = $('#' + param.otherproperty);
var StartDate = new Date(moment(otherProp.val(), 'MM/DD/YYYY'));
var Enddate = new Date(value);
if (StartDate != '')
{
return Enddate >= StartDate;
}
return false;
});
convert all of the MM/dd/yyyy to uppercase MM/DD/YYYY
so this should work
var otherProp = $('#' + param.otherproperty);
var StartDate = new Date(moment(otherProp.val(), 'MM/DD/YYYY'));
Related
Hi I would to like to compare string example 15/01/2017 with newDate()
var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() { //event date format is e.g 15/01/2017
return jQuery(this).text();
}).get();
var currentDate = new Date();
jQuery.each(dates, function (index, value) {
console.log(value);
//var parts = value.split('/');
//var mydate = new Date(parts[2],parts[0]-1,parts[1]);
//console.log("mydate is: "+mydate);
if(value < currentDate){
//do something
}
});
You just need to convert the current date to the same date format with which you are comparing.
var currentDate = new Date();
currentDate = ("0"+currentDate.getDate()).slice(-2) + "/" + ("0"+(currentDate.getMonth() + 1)).slice(-2) + "/" + currentDate.getFullYear();
Now your comparison with other values in dates should work fine.
Why you use less than condition inside if statement simply do this
var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() { //event date format is e.g 15/01/2017
return jQuery(this).text();
}).get();
var currentDate = new Date();
jQuery.each(dates, function (index, value) {
console.log(value);
var istrue = new Date();
currentDate = ("0"+currentDate.getDate()).slice(-2) + "/" + ("0"+
(currentDate.getMonth() + 1)).slice(-2) + "/" +
currentDate.getFullYear()=="15/01/2017";
if(istrue){
//do something
}
});
Although there are vanilla-javascript and Jquery-only based solutions, if your project is big enough I'd advice you to add moment.js to your project and use it for such comparisons.
It will make your life easier.
Check it out on the moment.js website
I have search date range 'from date' & 'to date' my validation function make data valid if range of date is not in same month ? for example if from date"2015-06-02" to date "2015-06-01" the range is invalid range? but if I make the to date "2015-05-31" it will be valid range
var validateDateRange = function () {
var fromDate = moment($scope.model.fromDateSearch, 'MM-DD-YYYY');
var toDate = moment($scope.model.toDateSearch, 'MM-DD-YYYY');
var a = (fromDate > toDate) ;
return a;
};
There is some problem with date format you are passing. So it would also work if you just remove the format and just pass in the value.
Run and check the code snippet below its working fine
var validateDateRange = function (fromDate, toDate) {
var fromDate = moment(fromDate);
var toDate = moment(toDate);
var a = (fromDate > toDate) ;
return a;
};
var fromDate= new Date("2015-06-02");
var toDate = new Date("2015-06-01");
alert(validateDateRange(fromDate, toDate));
var fromDate= new Date("2015-06-02");
var toDate = new Date("2015-05-31");
alert(validateDateRange(fromDate, toDate));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
You should use the number of milliseconds since the Unix Epoch, it is safer.
var validateDateRange = function () {
var fromDate = moment($scope.model.fromDateSearch, 'MM-DD-YYYY').valueOf();
var toDate = moment($scope.model.toDateSearch, 'MM-DD-YYYY').valueOf();
var a = (fromDate < toDate) ;
return a;
};
Note the 'lower than' sign.
I'm using bootstrap plugin as datepicker, I wrote a function to check whether it's today or not using pure js..
nothing happen, I wonder why.. the datepicker value is in format like dd/mm/yyyy
var datepicker = $('#datepicker').val();
checkToday(datepicker);
function checkToday(datepicker){
datepickerDay = datepicker.slice(0, datepicker.indexOf('/'));
var d = new Date();
var dToday = d.getDate() ;
dToday.toString();
if(datepickerDay == dToday){
return "Today"
alert('Today');
}
else{
return datepicker;
alert(datepicker);
}
}
Don't use parseInt Also you can't put anything after the return
var datepicker = $('#datepicker').val();
function checkToday(datepicker) {
datepickerDay = datepicker.slice(0, datepicker.indexOf('/'));
var d = new Date();
var dToday = d.getDate();
dToday.toString();
if (datepickerDay == dToday) {
return "Today";
} else {
return datepicker;
}
}
alert(checkToday(datepicker));
WORKING DEMO
You van convert both the dates in dates format then compare by date, month and year.
try this
function checkToday(datepicker){
var datepickerDay = new Date(datepicker); //creating date object from string 'mm/dd/yyyy'
var d = new Date(); // date object
// now lets comapare date, Month and year to get correct result
if(d.getDate()==datepicker.getDate() && d.getMonth()==datepicker.getMonth() && d.getYear()==datepicker.getYear()){
return "Today"
alert('Today');
}
else{
return datepicker;
alert(datepicker);
}
}
Hope this helps...
I have a date/time in an input (#myinput) using the following format:
yyyy/mm/dd hh:mm:ss
I need to compare this to todays date, so something like this...
var currentdate = new Date();
if(currentdate < $("#myinput").val()) {
alert("HELLO WORLD");
return false;
}
Any ideas?
I am using moment.js (http://momentjs.com/) for this kind of work.
Would look something like this:
var input = moment($("#myinput").val(), "YYYY/MM/DD HH:mm:ss");
if(input.diff(moment())>0){
...
If i had to use javascript only, i would use a regular expression to parse the date:
var dateString = "2012/12/05 12:00:01";
var regexp = /([0-9]{4})\/([0-9]{2})\/([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/;
var result = regexp.exec(dateString);
var date = new Date();
date.setYear(result[1]);
date.setMonth(result[2]-1);
date.setDate(result[3]);
date.setHours(result[4]);
date.setMinutes(result[5]);
date.setSeconds(result[6]);
if(new Date()<date){
...
You can use the Date.Parse method (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse) to compare the date inside your input against the current date or other dates in any kind of correct date format.
Ex:
var testDate = $('#myinput').val();
var d = new Date;
if (Date.parse(d)<Date.parse(testDate)) {
}
else{
}
Here is a fiddle: http://jsfiddle.net/uLybp/3/
use this function
//get full data now
function dateNow(){
var now = new Date(Date.now());
var dd=now.getDay() + "-" + (now.getMonth()+1) + "-" + now.getFullYear()+" ";
dd += now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
return dd;
}
example
var d=dateNow();
console.log(d); // 5-2-2021 1:35:24
Does anyone know how to parse date string in required format dd.mm.yyyy?
See:
Mozilla Core JavaScript Reference: Date object
Mozilla Core JavaScript Reference: String.Split
Code:
var strDate = "03.09.1979";
var dateParts = strDate.split(".");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
If you are using jQuery UI, you can format any date with:
<html>
<body>
Your date formated: <span id="date1"></span><br/>
</body>
</html>
var myDate = '30.11.2011';
var parsedDate = $.datepicker.parseDate('dd.mm.yy', myDate);
$('#date1').text($.datepicker.formatDate('M d, yy', parsedDate));
http://jsfiddle.net/mescalito2345/ND2Qg/14/
We use this code to check if the string is a valid date
var dt = new Date(txtDate.value)
if (isNaN(dt))
refs:
http://momentjs.com/docs/#/parsing/string/
If you use moment.js, you can use "string" + "format" mode
moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);
moment(String, String, String, Boolean);
ex:
moment("12-25-1995", "MM-DD-YYYY");
I'v been used following code in IE. (IE8 compatible)
var dString = "2013.2.4";
var myDate = new Date( dString.replace(/(\d+)\.(\d+)\.(\d+)/,"$2/$3/$1") );
alert( "my date:"+ myDate );
ASP.NET developers have the choice of this handy built-in (MS JS must be included in page):
var date = Date.parseLocale('20-Mar-2012', 'dd-MMM-yyyy');
http://msdn.microsoft.com/en-us/library/bb397521%28v=vs.100%29.aspx
Use Date object:
var time = Date.parse('02.02.1999');
document.writeln(time);
Give: 917902800000
This function handles also the invalid 29.2.2001 date.
function parseDate(str) {
var dateParts = str.split(".");
if (dateParts.length != 3)
return null;
var year = dateParts[2];
var month = dateParts[1];
var day = dateParts[0];
if (isNaN(day) || isNaN(month) || isNaN(year))
return null;
var result = new Date(year, (month - 1), day);
if (result == null)
return null;
if (result.getDate() != day)
return null;
if (result.getMonth() != (month - 1))
return null;
if (result.getFullYear() != year)
return null;
return result;
}
you can format date just making this type of the code.In javascript.
// for eg.
var inputdate=document.getElementById("getdate").value);
var datecomp= inputdate.split('.');
Var Date= new Date(datecomp[2], datecomp[1]-1, datecomp[0]);
//new date( Year,Month,Date)