Javascript - compare date/time now against yyyy/mm/dd hh:mm:ss - javascript

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

Related

Compare string dd/mm/yyyy with currentDate format js

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

How to manipulate a date that is fetched from an html input with javascript

I am trying to get a date from an html input to javascript, substract 3 years from it and then place the new date back into another html date input field.
I got the substraction to work with the current date but I can not get it to work when I fetch a date from an html input.
Below my code that works:
<input id="InputDate" data-role="datepicker"/>
<input id="OutputDate" data-role="datepicker"/>
<script type="text/javascript">
var dt = new Date(); // this line I want to replace with the next 2 lines!
//var inDate= document.getElementById("InputDate").value;
//var dt = new Date(inDate);
dt.setFullYear(dt.getFullYear() - 3);
var datestring = ("0" + dt.getDate()).slice(-2) + "." + ("0"+(dt.getMonth()+1)).slice(-2) + "." + dt.getFullYear();
document.getElementById("OutputDate").value = datestring;
When I fetch a date from the input field I get 'aN.aN.NaN' back into the output field.
I have tried document.getElementById("InputDate").valueAsDate instead, but this did not work either.
When I place an alert(dt) after the instantiation of dt I get 'Invalid Date'.
Any suggenstions how to get the fetched date in the DateObject correctly?
Regards, Manu
Just need to pass correct format to date() function:
//var dt = new Date(); // this line I want to replace with the next 2 lines!
var inDate= document.getElementById("InputDate").value;
inDate = inDate.split(".");
var dt = new Date(inDate[2],inDate[1],inDate[0]);
//alert(dt);
dt.setFullYear(dt.getFullYear() - 3);
var datestring = ("0" + dt.getDate()).slice(-2) + "." + ("0"+(dt.getMonth()+1)).slice(-2) + "." + dt.getFullYear();
document.getElementById("OutputDate").value = datestring;
<input id="InputDate" data-role="datepicker" value="19.05.2015"/>
<input id="OutputDate" data-role="datepicker"/>
var inputValues = document.getElementById("InputDate").value.split(".");
var dt = new Date(inputValues[2], inputValues[1] - 1, inputValues[0]);
If you get something like 22.12.1985 as InputDate value
first verify your ,document.getElementById("InputDate").value; is
returning correct date
var inDate= document.getElementById("InputDate").value;
var dt = new Date(Date.parse(inDate)); //use parse input to corret date format
dt.setFullYear(dt.getFullYear() - 3);
Use date format
var date = "2017-06-19";//yyyy-mm-dd or mm-dd-yyyy
var dt = new Date(date);
console.log(dt);
//output Mon Jun 19 2017 00:00:00 GMT+0545 (Nepal Standard Time)
// if you want to set the date to new output text box
var newDate = dt.getFullYear() + "-" + (dt.getMonth() + 1) + "-" + dt.getDate();
console.log(newDate);
var d = document;
d.g = d.getElementById;
var inDate = d.g("InputDate");
var outDate = d.g("OutputDate");
var datestring = "";
var mo = "";
var dt = null;
inDate.onchange = function() {
dt = new Date(inDate.value);
dt.setFullYear(dt.getFullYear() - 3);
datestring = ("0" + (dt.getMonth() + 1)).slice(-2) + "." + ("0" + dt.getDate()).slice(-2) + "." + dt.getFullYear();
outDate.value = datestring;
};
<input id="InputDate" data-role="datepicker" value="mm.dd.yyyy"><input id="OutputDate" data-role="datepicker" />
This revision makes use of the onchange event attribute which avoids the "NaN" message displaying in response to the default value of "mm.dd.yyyy" which serves as a prompt for the user. It also uses some short-cuts to reduce the verbosity of the code. Note: while the official example elegantly splits on a "." to get the date values, taking the time to use the Date methods to extract the month, day and year has an advantage despite the extra code. Splitting on a "." works as long as the input data contains a period to demarcate the month, day and year values. But the user could instead use a "/" or a "-" and then that splitting code would not yield the correct result, unless there was code that checked for the period.

Convert 2014-11-03T00:00:00 to yyyy-mm-dd in javascript

I have tried to change this date in yyyy-mm-dd using
function convert(str) {
var date = new Date(str);
var mnth = ("0" + (date.getMonth() + 1)).slice(-2)
var day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
But it's giving me the error Naan in i.e 8. It's working with all other browsers.
Any one can help me in this?
Thanks
You want to go from
2014-11-03T00:00:00
to
yyyy-mm-dd
you just need
function convert(str) {
return str.split("T")[0];
}
To create a date from one with a T and dashes, try
function convert(str) {
var parts = str.split("T");
var dParts = parts[0].split("-");
var tParts = parts[1].split(":");
return new Date(dParts[0],dParts[1],dParts[2],tParts[0],tParts[1],tParts[2]);
}
var d = convert("2014-11-03T00:00:00");
alert(d);

How to convert Date format in Javascript

I want to change date format sequence from yy-mm-dd to dd-mm-yy
How can I do it in Javascript ?
I have tried
var now = new Date();
now.format("mm-dd-yy");
But its not working for me
Here is a clear and simple approach
var now = new Date();
var dd = now.getDate(); //returns date
var mm = now.getMonth()+ 1; //returns month and you need to add1 because it is array
var yy = now.getFullYear(); //returns full year
var st = dd + '-' + mm + "-" + yy; //format as string
var dateFormatted = (now.getMonth()+1)+"-"+now.getDate()+"-"+now.getFullYear();
You can use the below mentioned function to format Date
utilities.FormatDate(new Date(),"GMT", "dd/MM/yyyy")
function dateformat(date)
{
var yourdate = new Date(date);
yourdate = yourdate.getDate() + '-' + yourdate.getMonth() +1 + "-" +yourdate.getFullYear();
}
use - or / as you like

Compare 2 dates in format DD/MM/YYYY with javascript/jquery

Suppose I receive two dates from the datepicker plugin in format DD/MM/YYYY
var date1 = '25/02/1985'; /*february 25th*/
var date2 = '26/02/1985'; /*february 26th*/
/*this dates are results form datepicker*/
if(process(date2) > process(date1)){
alert(date2 + 'is later than ' + date1);
}
What should this function look like?
function process(date){
var date;
// Do something
return date;
}
Split on the "/" and use the Date constructor.
function process(date){
var parts = date.split("/");
return new Date(parts[2], parts[1] - 1, parts[0]);
}
It could be more easier:
var date1 = '25/02/1985'; /*february 25th*/
var date2 = '26/02/1985'; /*february 26th*/
if ($.datepicker.parseDate('dd/mm/yy', date2) > $.datepicker.parseDate('dd/mm/yy', date1)) {
alert(date2 + 'is later than ' + date1);
}
For more details check this out. Thanks.
function process(date){
var parts = date.split("/");
var date = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
return date.getTime();
}

Categories