I want to check if date is today, then user should not be able to pick expired time. However, if the date selected is tomorrow, then any time can be selected.
I am trying to do a check in jquery, but not sure how to check. Below is the code used:
var targetTime = new Date().setMinutes(-5).valueOf();
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = mm + '/' + dd + '/' + yyyy;
alert(today);
if (jQuery('#startDatepicker').find("input").val() == today) {
alert("Yes");
var currentTime = jQuery('#startTimepicker').find("input").val();
alert(currentTime);
alert(targetTime);
if (currentTime <= targetTime) {
//Time Expired
alert("HI");
}
}
The date is in the format of "MM/DD/YYYY", and the time is in format of "hh:mm a". Expired time of 5 minutes is allowed. How to validate the expired time using jQuery?
Thanks
Use same Date object you have:
// .valueOf() gives time in milliseconds
var currentTime = new Date('25/12/2016 01:52').valueOf();
var targetTime = new Date().setMinutes(-5).valueOf();
if (currentTime <= targetTime) {
// expired
} else {
// not expired
}
Related
I have been trying to subtract date format (yyyy-MM-dd) to another date format but to no avail. I looked up online but it's confusing and I got this error which says that is it not a function for today.getDate(). Could anyone provide me with a solution in subtracting? I am at my wits end.
Thanks in advance.
for (var i = 0; i < 3; i++) {
var startDate = "2020-08-19"
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = yyyy + '-' + mm + '-' + dd;
today.setDate(today.getDate() - startDate);
console.log(today.getDate() - startDate);
if ((startDate - today) >= 30) {
console.log("hello");
} else if ((startDate - today) <= 30) {
console.log("bye");
}
};
You are mixing a few concepts:
var today = new Date();
This creates a Date object that wraps the number of milliseconds from 1/1/1970 at midnight UTC.
today = yyyy + '-' + mm + '-' + dd;
This is creating a string.
If your goal is to find out how many days are between two dates, your best bet is to use milliseconds since Unix epoch, sometimes called "Unix timestamps." With these timestamps, subtracting is straightforward since they're just numbers; you'll get the number of milliseconds between the two points of time. Getting from timestamps to number of days then becomes a matter of division:
const diff = Date.now() - Date.parse("2020-08-19");
const numDays = diff / (1000 * 60 * 60 * 24);
This question already has answers here:
Adding months to a Date in JavaScript [duplicate]
(2 answers)
Closed 4 years ago.
I have this code which works fine. It gives me todays date in a specific format.
function fetchTime() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var today = yyyy + '-' + mm + '-' + dd;
return (today);
}
I'm also trying to get today's date minus 1 month. I thought this would be simple, I just removed the +1. So I have this code:
function fetchTime() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var today = yyyy + '-' + mm + '-' + dd;
return (today);
}
This gives me the output 2019-00-17 which should be 2018-12-17
Can anyone tell me the right way to do this? My question is specific to getting the date out in the required format, whereas most examples I have seen do not output the right format as part of the date change.
I would separate the formatting from the fetching. You could make your existing formatting function take an optional parameter that defaults to today, so you could call it like you already were for today's date.
function formatTime(date) {
var dateToFormat = date || new Date();
var dd = dateToFormat.getDate();
var mm = dateToFormat.getMonth() + 1;
var yyyy = dateToFormat.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
return (yyyy + '-' + mm + '-' + dd);
}
Then you could also call it with today's date minus a month or any other date
formatTime(); //will default to today
var today = new Date();
formatTime(addMonths(today,-1)); //format last month's date
As pointed out by RobG in the comments you would need to implement an addMonths function as in Adding months to a Date in JavaScript
function addMonths(date, months) {
var d = date.getDate();
date.setMonth(date.getMonth() + +months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}
For substracting in moment.js:
moment().subtract(1, 'months').format("DD-MM-YYYY")
Documentation:
http://momentjs.com/docs/#/manipulating/subtract/
You should subtract months by getting the current month, then subtracting the number of months you want and then updating the date variable like this.
function fetchTime() {
var today = new Date();
today.setMonth(today.getMonth() - 1);
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var today = yyyy + '-' + mm + '-' + dd;
return (today);
}
Because your desired format is an ISO 8601 date, you could use JavaScript's .toISOString. You are only concerned with the first 10 characters though (not time), so you'd want to add .substring(0,10).
Date.prototype.toISODateString = function() { return this.toISOString().substring(0,10); }
Date.prototype.addMonths = function(val) { this.setMonth(this.getMonth()+val); return this;}
var date = new Date();
var todayFormatted = date.toISODateString();
console.log(todayFormatted);
var lastMonthFormatted = date.addMonths(-1).toISODateString();
console.log(lastMonthFormatted);
I've made the formatting steps a function called toISODateString() and added it to the Date prototype, which is a fancy way of saying "You can chain .toISODateString() to any Date now".
To set the date back a month, I've used .setMonth(). I also turned this into a function called addMonths.
Use the same code, but remove a month. Example:
function fetchTime() {
var today = new Date();
today.setMonth(today.getMonth() - 1);
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var today = yyyy + '-' + mm + '-' + dd;
return (today);
}
I'm not an expert in JavaScript, but I've been tasked with migrating a very large website that was coded into PHP into a new CMS that does not allow server-side languages. I am therefore spending my days converting many PHP date-calcs to JavaScript.
Something very simple in PHP:
<?php
if (date('md') < 816) {$award_year = date('Y');}
if (date('md') > 815) {$award_year = date('Y') + 1;}
/*echo "year ".$award_year;*/
?>
This allows the year an application is due to automatically change to next year after August 15.
I've been trying to recreate this effect with Javascript and here is what I have come up with:
var today = new Date();
var mm = today.getMonth()+1; //January is 0!
var dd = today.getday();
if (mm < 10) {
mm = '0'+mm
}
currDate = mmdd;
var currDate = new Date();
var appDate = new Date("0816");
if (currDate < appDate){
var printDate = theDate.getFullYear();
}
else if (currDate >= appDate){
var printDate = theDate.getFullYear()+1;
}
I know that I am missing something, because the var currDate cannot just = mmdd and then be compared to another date. Can someone help me with the next step here ? I'm trying to actually learn JavaScript as I go rather than just blindly fix issues.
This will look similar to your PHP code:
const date = new Date();
const day = date.getUTCDate();
const month = date.getMonth();
const md = month + "" + day;
if (md < 816) {
var award_year = date.getFullYear();
}
else if (md > 815) {
var award_year = date.getFullYear() + 1;
}
console.log(award_year);
Instead of combining strings and comparing two dates you could check the current day and month to determine which year to return.
For example,
function getAwardYear() {
const today = new Date()
const AUGUST = 7
if (today.getMonth() >= AUGUST && today.getDate() > 15) {
return today.getFullYear() + 1
}
return today.getFullYear()
}
// Today: 6th Jun 2018
getAwardYear() // 2018
// Today: 16th Aug 2018
getAwardYear() // 2019
I know there is plenty of things to improvise the below code... I am leaving it for you to improvise...
var today = new Date();
var mm = today.getMonth()+1; //January is 0!
var dd = today.getDay();
if (mm < 10) {
mm = '0'+mm
}
var yyyy="2018"
var currDate = new Date(yyyy+"-"+mm+"-"+dd);
var appDate = new Date("2018-01-16");
alert("appDate"+appDate);
alert("currDate"+currDate);
if (currDate < appDate){
var printDate = currDate.getFullYear();
alert(printDate);
}
else if (currDate >= appDate){
var printDate = appDate.getFullYear()+1;
alert(printDate);
}
I am trying to get specific date in java script.
What I am trying to do is.
I have variable called frequency Which can be Daily , Weekly and Monthly
if(frequency=="daily")
{
date='current_date -2 days';
}
else if ( frequency== 'weekly')
{
date='current_date -8 days';
}
else if ( frequency == 'monthly')
{
date='current_date -32 days';
}
This is the way I get current date with required format.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var hh = today.getHours();
var min = today.getMinutes();
var ss = today.getSeconds();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
// today = mm+'/'+dd+'/'+yyyy;
today = yyyy + "-" + mm + "-" + dd + " " + hh + ":" + mm + ":" + ss ;
alert("Today's date :"+today);
But, I am facing problem in subtracting the days
can anyone give an idea how can I achieve this.
You can simply do this:
function setDate(frequency) {
var today = new Date(),
date;
if (frequency == "daily") {
date = today.getTime() - (2*24*60*60*1000); // 2 days 24 hrs 60 mins 60 secs 1000 ms
} else if (frequency == 'weekly') {
date = today.getTime() - (8*24*60*60*1000);
} else if (frequency == 'monthly') {
date = today.getTime() - (32*24*60*60*1000);
}
return new Date(date);
}
document.querySelector('pre').innerHTML = setDate('monthly');
<pre></pre>
You need to convert days into time in ms miliseconds to convert it in time then you can subtract it from current time in ms after it you can return it as a new Date(date);.
You can use http://momentjs.com/ which makes date manipulation a breathe.
I am working on JavaScript validation where I am validating whether a textbox date is equal to the current date or not.
If its greater or equal to today's date than do something, if its less than today's date then show error message.
Note: In my textbox I have converted date into dd/MM/yyyy format. So I need to check textbox date with current date in dd/MM/yyy format only. Here is my code:
function ValidateDate() {
var EffectiveDate = $.trim($("[id$='txtFromDate']").val());
var Today = new Date();
if(EffectiveDate<Today())
{
//Show Error Message
}
else
{
//Do something else
}
I need the date to be in dd/MM/yyyy format for checking my textbox date, so my Today value has to be in dd/MM/yyyy format only.
function ValidateAddNewCourseCharge() {
var EffectiveDate = $.trim($("[id$='txtFromDate']").val());
var Today = new Date();
var dd = Today.getDate();
var mm = Today.getMonth() + 1; //January is 0!
var yyyy = Today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var Today = dd + '/' + mm + '/' + yyyy;
dateFirst = EffectiveDate.split('/');
dateSecond = Today.split('/');
var value = new Date(dateFirst[2], dateFirst[1], dateFirst[0]);
var current = new Date(dateSecond[2], dateSecond[1], dateSecond[0]);
if (EffectiveDate == "") {
showErrorMessagePopUp("Please Select a Date for Course Charge!");
return false;
}
else {
if (value < current) {
showErrorMessagePopUp("Date should not be less than Present Date!");
return false;
}
}
return true;
}
First, we have to find the current date -- below is code that finds it. Then, compare the result with the value entered in TextBox.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if(dd < 10) {
dd = '0' + dd
}
if(mm < 10) {
mm = '0' + mm
}
today = mm + '/' + dd + '/' + yyyy;
document.write(today);
var EffectiveDate = $.trim($("[id$='txtFromDate']").val());
I think it will help for you
var getdate = new Date($("[id$='txtFromDate']").val());
var curDate = new Date();
alert(getdate - curDate === 0);
alert(getdate - curDate < 0);
alert(getdate - curDate > 0);