Simple JS Question about date ( fullyear + 1) [duplicate] - javascript

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 3 years ago.
Trying to add a number new date in javascript
How ever the number is coming in from a json file.
Here is what i have.
myObj = {"yearsleft":"2", "name": "john"};
var term = myObj.yearsleft;
var d = new Date();
var year = d.getFullYear() + term.toString();
var month = d.getMonth()+1;
var day = d.getDate();
var output = ''+ (day<10 ? '0' : '') + day + '/' + (month<10 ? '0' : '') + month + '/' + year;
alert(output);
above is a working example
However its just appending 2 to the end of the year. which isnt what i want it to do.
I want it to add onto 2019
if that's possible.

You are adding string to a number, convert it to int and then add.
var year = d.getFullYear() + parseInt(term.toString(), 10);

Convert to a number - currently you're concatenating not adding.
myObj = {"yearsleft":"2", "name": "john"};
var term = myObj.yearsleft;
var d = new Date();
var year = d.getFullYear() + +term;
var month = d.getMonth()+1;
var day = d.getDate();
var output = ''+ (day<10 ? '0' : '') + day + '/' + (month<10 ? '0' : '') + month + '/' + year;
console.log(output);

d.getFullYear() + parseFloat(term);
Seem to have fixed it for me.

Related

Add 1 month to current date in JavaScript (Can you please integrate your solution/answer to this code and show) [duplicate]

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];

How can I get the value of a key from JSON to JS?

I'm working on an application that requires dates to be fed from JS and check in a JSON table what the "Letter" of the day is.
How can I take the JS date string that I've created (yyyy,mm,dd), and find the value of the corresponding key?
I have tried taking the string, stringifying it, and pushing it through a JS function.
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var dateStr = year + "/" + month + "/" + date;
console.log(dateStr);
dateJSON = JSON.stringify(dateStr)
console.log(dateJSON)
alert(testVar.dateJSON)
var testVar = { //In a JS file
"2019-11-06": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}
For "2019-11-08" I would like to have variable "letterDay" equal "A_con".
My code thus far returns "undefined" when I pull "testVar.dateJSON"
I think it is far simpler than this, your data keys are in format yyyy-mm-dd while you're dateStr is in the format yyyy/mm/dd here is a simple snippet
var testVar = { //In a JS file
"2019-11-09": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
// Your mistake was here, the separators were '/'
var dateStr = year + "-" + month + "-" + date;
console.log(dateStr);
// Get the value dynamically
// when you have a dynamic key use testVar[dateStr]
// testVar.dateStr will literally look for a key called "dateStr" it will not evaluate the value of dateStr
console.log(testVar[dateStr]);
You formatted your dateStr variable the wrong way ("/" instead of "-").
var testVar = { //In a JS file
"2019-11-06": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}
var d = new Date('November 06, 2019 23:15:30');
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var dateStr = year + "-" + month + "-" + (day > 9 ? day : '0' + day);
console.log(testVar[dateStr]); // D
Have your tried using JSON.parse()?
So: JSON.parse(testVar); and then modify your code and variables from there.

JavaScript format date like in PHP [duplicate]

This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
Closed 9 years ago.
I need to parse date from JSON (I can't do any change in this JSON on server).
{ ...
"time":"2014-02-14 18:37:48",
...
}
In php date() it is: YYYY-mm-dd HH:ii:ss
I want to change date format, for example to "dd.mm.YYYY HH:ii". In PHP it is easy, but in JavaScript I do not know how to parse it.
I try jQuery dateFormat, but I still do an error :-(
Can you please help me?
var arr=time.split(' ');
var date_arr=arr[0];
var time_arr=arr[1];
var temp_date=date_arr.split('-');
var temp_time=time_arr.split(':');
var js_date=temp_date[2]+'.'+temp_date[1]+'.'+temp_date[0]+' '+temp_time[0]+":"+temp_time[1];
You need to do all by your hands. Javascript's Date object has enough methods.
So please try smth like this:
var dateTime = new Date(Date.parse("2014-02-14 18:37:48"));
var date = dateTime.getDate().toString().length > 1 ? dateTime.getDate() : '0' + dateTime.getDate();
var month = dateTime.getMonth().toString().length > 1 ? dateTime.getMonth() + 1 : '0' + (dateTime.getMonth() + 1);
var hours = dateTime.getHours().toString().length > 1 ? dateTime.getHours() : '0' + dateTime.getHours();
var minutes = dateTime.getMinutes().toString().length > 1 ? dateTime.getMinutes() : '0' + dateTime.getMinutes();
var formattedDate = date + '.' + month + '.' + dateTime.getFullYear() + ' ' + hours + ':' + minutes;
console.log(formattedDate);

using date in javascript

I have trouble using date in Javascript, in PHP you use something like date("Y-m-d H:i s") to retrieve the specific date and time, how can I achieve this in Javascript? the getMonth() method only returns 1 digit, I really need them to be in 2 digits
Since I made comments on almost all answers, I'd better post my suggestion
DEMO
function pad(num) { return ("0"+num).slice(-2); }
function getDisplayDate() {
var date = new Date();
return date.getFullYear()+
"-"+pad(date.getMonth()+1)+
"-"+pad(date.getDate())+
" "+pad(date.getHours())+
":"+pad(date.getMinutes())+
":"+pad(date.getSeconds());
}
setInterval(function() {
document.getElementById("time").innerHTML=getDisplayDate();
},500);
why dont you add 0 before when you get <10
Try this :
function dateToYMD(date) {
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d) + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
}
var d = new Date();
alert(dateToYMD(d));
This code is adjusted based on the pointers given by #mplungjan -- (credits to him please)
Also check out his solution, which is a better one for use with date digits.
var str = 10; // month-digit from getMonth()
function pad(val) {
return "0" + val;
}
var month = str < 10 ? pad(str) : str;
console.log(month);
you can year, minutes, etc from Date class. You can get 2 digits month using some trick like example below. e.g
Date.prototype.getLongMonth = function() {
var month = this.getMonth();
if (String(month).length == 1) month = '0'.concat(month);
return month;
}
var now = new Date();
var theDate = now.getFullYear() + '-' + now.getLongMonth() + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
console.log(theDate); // result : 2013-02-17 12:41:2

Ajax get Date in dd/mm/yyyy format

var d = new Date();
var today_date = d.getDate() + '/' + month_name[d.getMonth()] + '/' + d.getFullYear();
This is how I am getting a date. It works with a slight problem. For todays date 7th of June 2011 it returns 7/11/2011, what i want it to return is 07/11/2011?
Anyone know how?
Well, you could simply check the length of d.getDate()and if it's 1 then you add a zero at the beginning. But you would like to take a look at format() to format your dates?
Like so:
("0"+1).slice(-2); // returns 01
("0"+10).slice(-2); // returns 10
Complete example:
var d = new Date(2011,1,1); // 1-Feb-2011
var today_date =
("0" + d.getDate()).slice(-2) + "/" +
("0" + (d.getMonth() + 1)).slice(-2) + "/" +
d.getFullYear();
// 01/02/2011
Try this (http://blog.stevenlevithan.com/archives/date-time-format):
var d = new Date();
d.format("dd/mm/yyyy");
Try this, this is more understandable.:
var currentTime = new Date();
var day = currentTime.getDate();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
if (day < 10){
day = "0" + day;
}
if (month < 10){
month = "0" + month;
}
var today_date = day + "/" + month + "/" + year;
document.write(today_date.toString());
And result is :
07/05/2011

Categories

Resources