This question already has answers here:
How to subtract days from a plain Date?
(36 answers)
Closed 8 years ago.
I'm trying to get the date 2 weeks before now.
this is my get today in the format I need it function:
function getTodayInFormat()
{
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getYear()-100;
return "20" + curr_year + "/" + curr_month + "/" + curr_date ;
}
Now lets say I do:
var today_date=getTodayInFormat();
How do I get two weeks before this date's date? In the same format? Thank you
Have you considered the plugin moment.js? I've used it to do things like this in the past and its fantastic and easy to use.
Create a function that you can pass a date object to, and subtract two weeks from that date, and return it in whatever format you need.
function two_weeks_ago(date_object) {
var two_weeks = 1209600000; // two weeks in milliseconds
var total = date_object.getTime() - two_weeks;
var date = new Date(total);
return date.getFullYear() +'/'+ (date.getMonth() + 1) +'/'+ date.getDate();
}
FIDDLE
Related
This question already has answers here:
How to add months to a date in JavaScript? [duplicate]
(3 answers)
Closed 2 years ago.
$(document).ready(function () {
//Init
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear() + "-" + (month) + "-" + (day);
$('#PaymentDate').val(today);
});
I'm new to JavaScript So can someone help me. using this code I can get the current date...But, how can I add one month to this date and get the next month date..
Currently I'm getting this - 12/30/2020 (Today's Date) Add 1 month Means I want to get - 01/30/2021 (Next month date).
Can you please integrate your solution/answer to this code and show
Try this
$(document).ready(function () {
//Init
var now = new Date();
// Add one month to the current date
var next_month = new Date(now.setMonth(now.getMonth() + 1));
// Manual date formatting
var day = ("0" + next_month.getDate()).slice(-2);
var month = ("0" + (next_month.getMonth() + 1)).slice(-2);
var next_month_string = next_month.getFullYear() + "-" + (month) + "-" + (day);
$('#PaymentDate').val(next_month_string);
});
You can also use this trick to get your YYYY-MM-DD style string instead of the manual formatting:
var next_month_string = next_month.toISOString().split('T')[0];
This question already has answers here:
convert date from dd/mm/yyyy to yyyy-mm-dd in javascript
(4 answers)
Closed 9 years ago.
How to convert date format in Javascript like
(MM-dd-YYYY) to (YYYY-MM-DD)
please suggest some standard format for conversion
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 newDate = curr_year + "-" + curr_month + "-" + curr_date ;
This should work fine
var str = "01-17-2021";
var arr = str.split("-");
var dateFormatted= arr[2]+"-"+arr[0]+"-"+arr[1];
Look at this link.
http://jsfiddle.net/9C2p8/
var start = new Date('10-10-2013');
var startDate = $.datepicker.formatDate('yy-mm-dd', new Date(start));
alert(startDate);
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
how to convert timeInMs to this format dd/mm/yyyy
var timeInMs = Date.now();
alert(timeInMs);
//convert to dd/mm/yyy
is there any advantage of using jquery when dealing with time/date?
var date = new Date();
var year = date.getUTCFullYear();
var month = date.getUTCMonth();
var day = date.getUTCDate();
//month 2 digits
month = ("0" + (month + 1)).slice(-2);
//year 2 digits
year = year.toString().substr(2,2);
var formattedDate = day + '/' + month + "/" + year;
prints 25/10/13
This question already has answers here:
How to create a date object from string in javascript [duplicate]
(8 answers)
Closed 9 years ago.
I want to assign date to new date object. when I am using it is not giving correct result http://jsfiddle.net/Cqw5c/
new Date (12/12/2012)
function getD() {
var d = new Date("12/12/2012");
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();
alert( month + "/" + day + "/" + year);
}
Update
HTML:
$('#clickme').click(function() {
var thetestdate = $('#testdate').val();
mynewdate = getD(thetestdate);
alert(mynewdate);
});
JS:
function getD(mydatevariable) {
var d = new Date(mydatevariable);
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();
var wholedate = ( month + "-" + day + "-" + year);
return wholedate;
}
Quote the argument:
var d= new Date("12/12/2012");
Here's an updated fiddle. You're currently creating a date with the result of the expression 12/12/2012, which is three numbers and two division operators:
console.log(12 / 12 / 2012); // 0.0004970178926441351
You're creating a new date based on
(12 / 12) / 2012 == 0.0004970178926441351
which will be some fraction of a second after January 1, 1970.
You can create a Javascript date by parsing a string:
new Date( "12/12/2012");
or by passing in individual components:
new Date( 2012, 12, 12 );
See Date at MDN for more variations.
This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 9 years ago.
I want to add 1 Month or 6 Month to a given Date.
But if i add one Month, the year isnt incremented. And if i add 6 Month to June, i got the Month 00 returned BUT the year is incremented.
Could you please help me out?
function addToBis(monthToAdd){
var tmp = $("#terminbis").val().split('.');
var day = tmp[0];
var month = tmp[1];
var year = tmp[2];
var terminDate = new Date(parseInt(year),parseInt(month), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
day = "";
month = "";
year = "";
if(terminDate.getDate() < 10){
day = "0"+terminDate.getDate();
} else{
day = terminDate.getDate();
}
if(terminDate.getMonth() < 10){
month = "0"+terminDate.getMonth();
} else{
month = terminDate.getMonth();
}
year = terminDate.getFullYear();
$("#terminbis").val(day+"."+month+"."+year);
}
getMonth returns a number from 0 to 11 which means 0 for January , 1 for february ...etc
so modify like this
var terminDate = new Date(parseInt(year),parseInt(month - 1), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
and
month = terminDate.getMonth() + 1;
You should use the javascript Date object's native methods to update it. Check out this question's accepted answer for example, it is the correct approach to your problem.
Javascript function to add X months to a date
The function can be written much more concisely as:
function addToBis(monthToAdd){
function z(n) {return (n<10? '0':'') + n}
var tmp = $("#terminbis").val().split('.');
var d = new Date(tmp[2], --tmp[1], tmp[0]);
d.setMonth(d.getMonth() + monthToAdd);
$("#terminbis").val(z(d.getDate()) + '.' + z(d.getMonth() + 1)
+ '.' + d.getFullYear();
}
The value of terminbis and monthToAdd should be validated before use, as should the date generated from the value.