My date is in this format 2008-01-01(yyyy-mm-dd). I would like to add months to add format such that if I have 2008-01-01 + 13 would give me 2009-02-01. How can this be done in javascript?
var d = new Date('2008-01-01');
d.setMonth( d.getMonth() + 1 );
alert( d.getFullYear()+'-'+(d.getMonth() + 1 ) + '-'+ d.getDate());
You can add months to your date this way.
new Date(new Date(myDate).setMonth(myDate.getMonth()+13));
Simple as that:
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 13);
This is my own answer.
var moment = require('moment')
var myDate = new Date("2008-01-01");
myDate.setMonth(myDate.getMonth() + 13);
var answer = moment(myDate).format('YYYY-MM-DD');
Related
<code>
var d2 = $('#interval').val();
var new_date = new Date(get_start_date);
new_date.setDate(new_date.getDate() + d2);
var dd = new_date.getDate();
var mm = new_date.getMonth() + 1;
var y = new_date.getFullYear();
var endDate = y + '-' + mm + '-' + dd;
</code>
assuming d2 = 5
when im adding 5 dates to my current date, its not returning exact answer instead its adding months it becomes 2017-09-09
but when i just do this new_date.setDate(new_date.getDate() + 5) it gives me the correct output.
Just parse you d2 as an integer
var d2 = parseInt($('#interval').val(),10);
u can only use this arguments
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
get_start_date is invailid ;)
var new_date = new Date(get_start_date);
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
The question is simple, how can I add days to a date in YYYY-MM-DD format?
For example, increment a date given in this format: "2013-02-11" to "2013-02-12"?
date = new Date('2013-02-11');
next_date = new Date(date.setDate(date.getDate() + 1));
here's a demo http://jsfiddle.net/MEptb/
Hope below code will helpful to you
function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}
var myDate = new Date('2013-02-11');
var newDate = addDays(myDate,5);
Something like this :
var date = new Date('2013-02-11');
/* Add nr of days*/
date.setDate(date.getDate() + 1);
alert(date.toString());
I hope it helps.
Below function is to Add number of days to today's Date
It returns the Incremented Date in YYYY-MM-DD format
#param noofDays - Specify number of days to increment. 365, for 1 year.
function addDaysToCurrentDate(noofDays){
date = new Date();
next_date = new Date(date.setDate(date.getDate() + noofDays));
var IncrementedDate = next_date.toISOString().slice(0, 10);
console.log("Incremented Date " +IncrementedDate );
return IncrementedDate;
}
With the date parameter you can add your required date and you can add days with the addDays() function:
var start_date = new Date('2013-02-11');
var next_date_update = addDays(start_date,1);
var next_date = new Date(next_date_update).toLocaleDateString('en-CA');
if(next_date!='Invalid Date')
{
var final_date = next_date;
console.log(final_date);
}
function addDays ( myDate , days)
{
return new Date(myDate.getTime() + days*24*60*60*1000);
}
hope it will help you out
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formatting a date in JavaScript
I need to show a current date in format like this (some examples):
sep 10, 2012
nov 5, 2012
and so on.
Using javascript I get a current date object
var date = new Date();
what I need to do next?
you can use this.
function dateTest(){
var d =new Date();
var month_name=new Array(12);
month_name[0]="Jan"
month_name[1]="Feb"
month_name[2]="Mar"
month_name[3]="Apr"
month_name[4]="May"
month_name[5]="Jun"
month_name[6]="Jul"
month_name[7]="Aug"
month_name[8]="Sep"
month_name[9]="Oct"
month_name[10]="Nov"
month_name[11]="Dec"
alert(month_name[d.getMonth()]+" "+d.getDate()+" , "+d.getFullYear());
}
You can use the getMonth (including some switch/case for the text), the getDate and the getFullYear methods to build your string.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/prototype#Methods
Use dateFormat lib available in following link
http://stevenlevithan.com/assets/misc/date.format.js
Refer this article for formatting js using above lib.
http://blog.stevenlevithan.com/archives/date-time-format
Edit:
If you cant use lib then
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var formattedDate = curr_date + " " + curr_month + ", " + curr_year;
Try this
function getCurrentDate(){
var now=new Date();
var date=now.getDate();
var year=now.getFullYear();
var months=new Array('jan', 'feb', 'mar' ... 'dec');
var month=months[now.getMonth()]
return month + ' ' + date + ', ' + year;
}
EDITED
You can use this function in your code :
function getFormattedDate(input){
var pattern=/(.*?)\/(.*?)\/(.*?)$/;
var result = input.replace(pattern,function(match,p1,p2,p3){
var months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Dec'];
return (months[(p1-1)]+" "+p2<10?"0"+p2:p2)+" "+p3;
});
alert(result);
}
And for ref. you can call this function directly like this:
getFormattedDate("11/18/2013");
OR you can use this code, too.
var date = new Date();
dateFormat(date,"mediumDate");
you can also find further different formats here.
How can I get the current date in Javascript in this format?
"M/D/YYYY"?
Thanks.
If this would be today it would be
"2/17/2011", if it was the 3rd it would be "2/3/2011".
Thanks
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
I assigned each part into its own variable for this example so that it's more clear as to what it returns.
Use the javascript Date object:
var d = new Date();
alert((d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear());
Steven Levithan's (stevenlevithan.com) dateFormat function looks really versatile to me.
See:
http://blog.stevenlevithan.com/archives/date-time-format
In his code, he adds dateFormat in as a prototype method for Date.
// For convenience...
Date.prototype.format = function (mask, utc) {
return dateFormat(this, mask, utc);
};
So you can use it as a method on a Date object.
var now = new Date();
var variable=now.format("m/dd/yy");
document.write(variable);
HTH
Rich