I am trying to expire the content when a given date has been reached. If the date has not been reached, I would like the content not to have expired.
So currently, in my css, I have both demo and demo1 set to display:block;
Then I do the following:
<p id="demo">hi</p>
<p id="demo1">hi2</p>
<script>
var exp1 = "02/13/2018";
var exp2 = "02/15/2018";
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var now = curr_month + "/" + curr_date + "/" + curr_year;
if(now == exp1)
document.getElementById("demo").style.display = "none";
else{
if(now == exp2)
document.getElementById("demo1").style.display = "block";
}
</script>
However, when I run the code, both paragraphs display their content rather then only the <p> tag that has hi2 that should only appear. Any assistance would be appreciated.
You are not adding leading zero to your month and day value when its value is less than 10. You can add leading zeroes to number less than 10 by checking if month and day value is less than 10 and adding leading zeroes.
var exp1 = "02/13/2018";
var exp2 = "02/15/2018";
var d = new Date();
var curr_date = d.getDate();
if(curr_date < 10)
curr_date = '0' + curr_date;
var curr_month = d.getMonth() + 1; //Months are zero based
if(curr_month < 10)
curr_month = '0' + curr_month;
var curr_year = d.getFullYear();
var now = curr_month + "/" + curr_date + "/" + curr_year;
if(now == exp1)
document.getElementById("demo").style.display = "none";
else{
if(now == exp2)
document.getElementById("demo1").style.display = "block";
}
<p id="demo">hi</p>
<p id="demo1">hi2</p>
You can also use date#toISOString and string#replace to generate the date in MM/DD/YYYY format.
var exp1 = "02/13/2018";
var exp2 = "02/15/2018";
var date = new Date();
now = date.toISOString().substring(0,10).replace(/(\d+)-(\d+)-(\d+)/,'$2/$3/$1')
if(now == exp1)
document.getElementById("demo").style.display = "none";
else{
if(now == exp2)
document.getElementById("demo1").style.display = "block";
}
<p id="demo">hi</p>
<p id="demo1">hi2</p>
Related
I am creating a script to enter date in some specific field, the format it requires is MMM DD, YYYY, as you can see format has 2 spaces one between month name and date and other between comma and year. I searched so many places and tried below code but it returns value as NaN, 2018 my code is listed below-
this.getCurrentDate = function () {
var d = new Date();
var currentDate = d.getDate();
var currentMonth = d.getMonth()+1;
var currentYear = d.getFullYear();
if (currentDate < 10){
currentDate = '0'+currentDate;
}
if (currentMonth < 10){
currentMonth = '0'+currentMonth;
}
var today = currentMonth + '\xa0' + currentDate-1 + ',' + '\xa0' + currentYear;
console.log(today);
return today;
};
Your issue lies with this line of code:
var today = currentMonth + '\xa0' + currentDate-1 + ',' + '\xa0' + currentYear;
So previously you set these two variables to strings:
if (currentDate < 10){
currentDate = '0'+currentDate;
}
if (currentMonth < 10){
currentMonth = '0'+currentMonth;
}
In the variable today at currentDate you're trying to subtract the integer 1 from a string. Hence, NaN (Not a Number).
You are subtracting 1 from a string which is causing error.
this.getCurrentDate = function () {
var d = new Date();
var currentDate = d.getDate();
var currentMonth = d.getMonth()+1;
var currentYear = d.getFullYear();
if (currentDate < 10){
currentDate = '0'+currentDate;
}
if (currentMonth < 10){
currentDate--; //Fixed here.
currentMonth = '0'+currentMonth;
} //removed -1 from current date
var today = currentMonth + '\xa0' + currentDate + ',' + '\xa0' + currentYear;
console.log(today);
return today;
};
You have to subtract value from a number before you change it to a string. This will work now.
var d = new Date();
var n = d.toLocaleString();
document.getElementById("demo").innerHTML = n;
How I can get only Date?
Now I get 3/2/2016, 1:34:07 PM.
I want to get 3/2/2016;
Try this:
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var n = curr_month + "/" + curr_date + "/" + curr_year;
document.getElementById("demo").innerHTML = n;
Objective: Setup the date in the array at the end of the block to read: yyyymmdd including the zeros. (example data build: numDaysAPITimes = [20150403]). What I got is not getting the month correctly and the numDaysAPITimes array is storing only the year for some reason.
var totalPrecipSinceDate;
var numDaysAPITimes = [];
var userDataDatePick = document.getElementById('dateRngPick').value;
if (userDataDatePick >=1)
{
for (var i = 0; i <= (userDataDatePick-1); i++) //place user userData-1 where i <= input
{
var myDate = new Date(); //http://stackoverflow.com/questions/7693170/javascript-convert-from-epoch-string-to-date-object
var epoch = myDate.getTime();
var unixEpoch = Math.round(epoch/1000)
var backDateEpochTime = Math.round(unixEpoch - (86400 * i)); //Get each day (UNIX seconds)
var d = new Date(backDateEpochTime); //Convert to UTC
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
numDaysAPITimes[i] = (curr_year + curr_month + curr_date);
}
}
else
{
alert("You have not entered a valid number for the date.");
numDaysAPITimes.length = 0;
}
a couple things:
your date info is getting added together as numbers, that why it seems the year is only going through. One way to handle that would be to use the toString() method.
you'll probably want leading zeroes on the day and month, which you can achieve by prepending a 0 and then doing a slice -2.
That would looks like This JSFiddle, or:
var totalPrecipSinceDate;
var numDaysAPITimes = [];
var userDataDatePick = 2;//document.getElementById('dateRngPick').value;
if (userDataDatePick >=1)
{
for (var i = 0; i <= (userDataDatePick-1); i++) //place user userData-1 where i <= input
{
var myDate = new Date(); //http://stackoverflow.com/questions/7693170/javascript-convert-from-epoch-string-to-date-object
var epoch = myDate.getTime();
var unixEpoch = Math.round(epoch/1000)
var backDateEpochTime = Math.round(unixEpoch - (86400 * i)); //Get each day (UNIX seconds)
var d = new Date((1000*backDateEpochTime)); //Convert to UTC
var curr_date = ("0" + d.getDate()).slice(-2)
var curr_month = ("0"+ (d.getMonth() + 1)).slice(-2); //Months are zero based
var curr_year = d.getFullYear();
console.log(d.getMonth());
numDaysAPITimes[i] = (curr_year.toString() + curr_month.toString() + curr_date.toString());
}
}
else
{
alert("You have not entered a valid number for the date.");
numDaysAPITimes.length = 0;
}
console.log(numDaysAPITimes)
for(i=0;i<30;i++){
}
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
I'm stuck, how can I print out the next 30 days?
like 3/3/2015, 4/3/2015, 5/3/2015 and so on..
var d = new Date();
var curr_month = d.getMonth();
curr_month = curr_month + 1;
var curr_date = d.getDate();
for( i = curr_date; i <= 31; i++ ){
var curr_year = d.getFullYear();
var date = curr_date + '/' + curr_month + '/' + curr_year;
curr_date++;
console.log(date);
}
Try this.It worked for me.
demo link
Try this:
var d = new Date();
var curr_day = d.getDate();
for(i=0;i<30;i++){
curr_day = curr_day+1;
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var newDate = new Date(curr_year,curr_month,curr_day)
var formatedDate = newDate.getDate() + '/' + newDate.getMonth() + '/' + newDate.getFullYear();
formatedDate;
}
You need to increment the day variable
You could put all that code below the loop inside the loop, then for each iteration add one day to the Date object. (add 24 * 60 * 60 * 1000)
var ONE_DAY = 24 * 60 * 60 * 1000;
var d = new Date();
for(i=0;i<30;i++) {
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
d = new Date(d.getMilliseconds() + ONE_DAY);
}
I would probably make slightly different style choices, but this is one way of doing it that pertains to the code you wrote.
Also, I don't have enough reputation to comment, but the other solution by pedrumj is very similar to mine, but keep in mind that it actually skips the first day (the new date should be assigned at the end of the loop)
Demo here
Use this
var d = new Date();
var curr_month = d.getMonth();
for(i=0;i<30;i++){
var curr_date = d.getDate();
var curr_month = curr_month + 1;
var curr_year = d.getFullYear();
var Printdate = curr_date + '/' + curr_month + '/' + curr_year;
console.log(Printdate);
}
You can reduce the date field by 1 day on each loop as follows:
var d = new Date(1425356823380);
for(i = 0; i < 30; i++){
var date = d.toLocaleDateString();
// code
d.setDate(d.getDate()+1);
}
Here is a fiddle
Your for loop doesn't loop through anything. You should set your fixed variables first, then loop through your variable you wish to change.
for(i=0;i<30;i++){
}
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
Like this:
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var curr_day = d.getDay();
var formatedDate = curr_month + '/' + curr_day + '/' + curr_year;
for(i=0;i<30;i++){
curr_day++;
formattedDate = curr_month + '/' + curr_day + '/' + curr_year;
console.log(formattedDate);
}
Mind, February only has 29 days at most.
See the Docs for more information. You may just want to use a library that can handle all the strange edge cases that happen with dates, such as MomentJS
for(var i=0;i<30;i++){
var start = new Date();
var next = new Date();
next.setDate(start.getDate() + i);
var date_str=next.getDay()+"/"+next.getMonth()+"/"+next.getFullYear()
console.log(next);
}
You can format start, next date as per your need.
i have a start Time and end time in string. i have split start time (time string format "05:30:00")
var dateArray = data.start_time.split(":");
var firstHour = dateArray[0];
var firstMinutes = dateArray[1];
var firstSec = dateArray[2];
now i want to Add 80 minutes to this time and want generate a new time (push in a array) , then i want to add it again n again to a end time limit how it is possible?
why not use check:
var slog=80;
var firstHour = dateArray[0];
var firstMinutes = dateArray[1];
var firstSec = dateArray[2];
if(slog%60 != 0){// when slog is not fixed
firstHour=firstHour+slog/60;
firstMinutes=firstMinutes+slog%60;
}else if(slog%60 == 0){
firstHour=firstHour+slog/60;
}
From the top of my head (not tested!):
yourTime = startTime;
while(yourTime < endTime){
yourTime.setMinutes(yourTime.getMinutes() + 80);
dateArray.push(yourTime);
}
var d = new Date();
var theDate = d.getFullYear() + '-' + ( d.getMonth() + 1 ) + '-' + d.getDate();
var theTime = theDate + " 15:00:00";
var newTime = new Date( Date.parse( theTime ) + 80*60*1000 );