Get onldey date from DateTime - javascript

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;

Related

Expire Content when Date has arrived Javascript

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>

Split Date.now() into parts in Javascript

I am trying to split the current date into parts and this is what I am doing
var today = new Date();
var currDay = today.getDay();
var currMonth = today.getMonth();
var currYear = today.getYear();
alert(currDay + "/" + currMonth + "/" + currYear)
but with this I get the following result
0/7/115 (output)
where it should be
09/07/2015
What can I do to get the required result?
Your "today.getDay()" should be "today.getDate()" since .getDay goes from 0-6 (days of the week)
var today = new Date();
var currDay = today.getDate();
var currMonth = today.getMonth();
var currYear = today.getFullYear();
alert(currDay + "/" + currMonth + "/" + currYear);
Try
var currYear = today.getFullYear();
Based on the answer from https://stackoverflow.com/a/3605248/2649340:
var today = new Date();
var output = ('0' + today.getDate()).slice(-2) + '/'
+ ('0' + (today.getMonth()+1)).slice(-2) + '/'
+ today.getFullYear();
getDay() gives you the day's number (0-6 for sunday to monday), month starts with 0 for january. This is how to do:
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
if(day<10) {
day='0'+day;
}
if(month<10) {
month='0'+month;
}
today = month+'/'+day+'/'+year;
alert(today);
you can use moment.js. It has a lot of helpful functions for time and date in JavaScript

loop through date and print formated date

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.

Add day(s) in a date in Javascript

I have textbox displaying date and have a button. the function in button is to add 7 days and display in textbox. my code:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}
The issue how to add a day to go to the next month.
Example the date in Textbox is 2014/08/25 when I click the button the date will be 2014/09/01
Just add 7 days to your date, date already handles change of month/year:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
addday.setDate(addday.getDate() + 7);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}
If you just do this
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
that means if date is 21.12.2014 the output will be 28.13.2014
function onNext() {
var startdate = document.getElementById('date').value;
var d2 = new Date(startdate);
d2.setMonth(d2.getMonth()+1);
d2.setDate(1); // you can set here whatever date you want
document.getElementById('date').value = d2.getFullYear() + '/' + d2.getMonth() + '/' + d2. getDate();
}
Use this function
function updateAb(s){//format dd/mm/yyyy chnage according to your need
var dmy = s.split("/");
var joindate = new Date(
parseInt(dmy[2], 10),
parseInt(dmy[1], 10) - 1,
parseInt(dmy[0], 10)
);
var data_days=7;
joindate.setDate(joindate.getDate() + data_days);
var cc=("0" + joindate.getDate()).slice(-2) + "/" +("0" + (joindate.getMonth() + 1)).slice(-2) + "/" +joindate.getFullYear();
document.getElementById("datepickerdisabled1").value=cc;
}

How to split the date , time using javascript?

How to split the date,time using java script ? I am getting date,time 2010-05-04 20:13:12.0.But i need only date.
date format mm/dd/yyyy
var cdate = gridline[i=1]["CDATE"];
Now i need cdate as mm/dd/yyyy only.
Please help me.
function getDateFromString(datestring) {
var d = new Date(datestring);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
return curr_date + "-" + curr_month + "-" + curr_year;
}
Source: webdevelopersnotes.com

Categories

Resources