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);
}
Related
I have a small problem. I have a working code for adding days depending on the current time (if it is before 10am it adds 1 day if it is after 10am it adds 2 days for the date). So here is my question: How can I edit the code so it will skip the weekends in the calculations? For example, if I execute it before 10am on Friday it should show me the Monday date, not Saturday or Sunday.
function onGetMultiValue() {
var dzis = new Date();
var Godzina = new Date().getHours();
var teraz = dzis.getDate();
if (Godzina < 10) {
dzis.setDate(dzis.getDate() + 1);
} else {
dzis.setDate(dzis.getDate() + 2);
}
var day = dzis.getDate();
var month = dzis.getMonth() + 1;
var year = dzis.getFullYear();
var praca = day+"."+month+"."+year;
return praca;
}
You can use getDay() to check which day of the week it is.
function onGetMultiValue() {
const today = new Date();
const hour = new Date().getHours();
const dayOfWeek = today.getDay();
if (dayOfWeek == 5) {
// if it's Friday add 3 days to the date
today.setDate(today.getDate() + 3);
}
today.setDate(today.getDate() + hour < 10 ? 1 : 2)
const day = today.getDate();
const month = today.getMonth() + 1;
const year = today.getFullYear();
return `${day}.${month}.${year}`;
}
console.log(onGetMultiValue())
Can someone help me determine why this code is not working?
<script type="text/javascript">
const date = new Date();
const day = date.getUTCDate();
const month = date.getMonth(); //January is 0!
const md = month + "." + day
if (md < 3.03) {
var theDate = new Date();
var award_year1 = date.getFullYear();
var award_year2 = date.getFullYear() + 1;
}
else if (md > 3.03) {
var theDate = new Date();
var award_year1 = date.getFullYear() + 1;
var award_year2 = date.getFullYear() + 2;
}
console.log(award_year1);
console.log(award_year2);
</script>
Today is June 7, so it should return award_year1 = 2019 and award_year2 = 2020.
Just a suggestion to make your code a little more declarative/ easier to read. You can compare 2 date objects in the way you would expect to.
const date = new Date();
let offset = 0;
const threshold = new Date();
threshold.setMonth(3);
threshold.setDate(3);
if (Date.now() > threshold) { //might be a bit easier to reason about
offset = 1;
}
var theDate = new Date();
var award_year1 = date.getFullYear() + offset;
var award_year2 = date.getFullYear() + 1 + offset;
console.log(award_year1);
console.log(award_year2);
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);
I have a form on my site that should validate for anyone who is over 18.
var day = $("#dobDay").val();
var month = $("#dobMonth").val();
var year = $("#dobYear").val();
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);
var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
var output = currdate - mydate
if ((currdate - mydate) > 0){
// you are not 18
}
But it working totally opposite way. I would like the if statement to take action when user is over under 18 years old.
Thank you for your help in advance
check this DEMO
var day = 12;
var month = 12;
var year = 2006;
var age = 18;
var setDate = new Date(year + age, month - 1, day);
var currdate = new Date();
if (currdate >= setDate) {
// you are above 18
alert("above 18");
} else {
alert("below 18");
}
var day = $("#dobDay").val();
var month = $("#dobMonth").val();
var year = $("#dobYear").val();
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);
var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
if(currdate < mydate)
{
alert('You must be at least 18 years of age.');
}
Here is a somewhat lighter version that I tested:
var day = 1;
var month = 1;
var year = 1999;
var age = 18;
var cutOffDate = new Date(year + age, month, day);
if (cutOffDate > Date.now()) {
$('output').val("Get Outta Here!");
} else {
$('output').val("Works for me!");
}
The key is to add the minimum age to the birthdate and confirm that it is before the current date. You are checking if the current date minus the minimum age (basically the latest birthdate allowed) was greater than than the birthdate provided, which will give you the reverse.
18 year old validation rule for jQuery Validator plugin using addMethod function.
jQuery.validator.addMethod(
"validDOB",
function(value, element) {
var from = value.split(" "); // DD MM YYYY
// var from = value.split("/"); // DD/MM/YYYY
var day = from[0];
var month = from[1];
var year = from[2];
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);
var currdate = new Date();
var setDate = new Date();
setDate.setFullYear(mydate.getFullYear() + age, month-1, day);
if ((currdate - setDate) > 0){
return true;
}else{
return false;
}
},
"Sorry, you must be 18 years of age to apply"
);
and
$('#myForm')
.validate({
rules : {
myDOB : {
validDOB : true
}
}
});
if it's working the opposite way have you tried swapping the > for a < on the second to last line?
I think it will be easier to understand if we rename the variables
mydate => givenDate
currdate => thresholdDate
if givenDate > thresholdDate => you are not 18
else => you are 18
i.e.
if ( givenDate > thresholdDate ){
// you are not 18
}
i.e
if ((givenDate - thresholdDate) > 0){
// you are not 18
}
i.e.
if ((mydate - currdate ) > 0){
// you are not 18
}
I have the following code snippet which is giving me fits. I just want to fetch the latest date, instead I get 12/31/1969 4:00 PM. Code is below, any/all help greatly appreciated.
if (setPass) {
var lastDate = new Date().toJSON();
for (var k = passVal.Values.length - 1; k >= 1; k--) {
if (passVal.Values[k].Average !== passVal2.Values[k].Average) {
lastDate = passVal.Values[k].Date.toUTCString();
break;
}
}
}
var passView = {
ID: id,
lastDate: lastDate
};
results.push(passView);
}
The format you need that is a String, So you you can build your own one. Like,
var date = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate();
var month = new Date().getMonth() < 10 ? '0' + new Date().getMonth() : new Date().getMonth();
var year = new Date().getFullYear();
var fullYear = month +'/'+ date +'/'+ year ;
console.log(fullYear);
then you can pass it. Cheers :).