I want to create the JSON formatted as shown below, in decreasing order starting from current date (it will be the actual current date).
var theMonths = [
{"date":"2015-12","value":null}, {"date":"2016-01","value":null},
{"date":"2016-02","value":null}, {"date":"2016-03","value":null},
{"date":"2016-04","value":null}, {"date":"2016-05","value":null},
{"date":"2016-06","value":null}, {"date":"2016-07","value":null},
{"date":"2016-08","value":null}, {"date":"2016-09","value":null},
{"date":"2016-10","value":null}, {"date":"2016-11","value":null},
{"date":"2016-12","value":null}, {"date":"2017-01","value":null},
{"date":"2017-02","value":null}, {"date":"2017-03","value":null},
{"date":"2017-04","value":null}, {"date":"2017-05","value":null}
]
I want to pass this data in a chart.
var dateFromPast = [];
var amountOfDatesFromPast = 18;
for (var i = 0; i < amountOfDatesFromPast; i++){
var dateObj = new Date();
dateObj.setMonth(dateObj.getMonth() - i);
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var year = dateObj.getUTCFullYear();
newdate = year + "-" + month;
dateFromPast.push(newdate);
}
console.log(dateFromPast);
Related
I'm wondering how to create single loop for create variable between today and start date.
var startDate = 11-15; /*that means 11th month of 2015*/
var d = new Date();
var m = d.getMonth() + 1;
var y = d.getFullYear().toString().substr(-2);
for (var i = 11; i <= m; i++) {
console.log(i);
}
Result should be 11-15, 12-15, 1-16 and so on until today 9-17. I don't know how I can add year into my code.
Assuming startdate is a string you can use below code
var startDate = "11-15"
var d = new Date();
var m = d.getMonth() + 1;
var y = d.getFullYear().toString().substr(-2);
d=startDate.split("-")
counter = parseInt(d[0])
for(var i=parseInt(d[1]);i<=parseInt(y);i++)
{
for(var j=counter;j<=12;j++){
if(j>m && i==y){
continue
}
console.log(j+"-"+i)
}
counter = 1;
}
Well you can use .split() over startDate which should be a string, and extract month and year and keep incrementing them respectively until you reach 09-17:
var startDate = "11-15";
var month = parseInt(startDate.split("-")[0]);
var year = parseInt(startDate.split("-")[1]);
var results = [];
while(!(year === 17 && month === 9)){
if(month<12){
month++;
}else{
month = 1;
year++;
}
console.log(month+'-'+year);
results.push(month+'-'+year);
}
console.log(results);
I will explain my question in the code itself. Please see the below code
var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var ctdate = (new Date()).getMonth() + 1;// getting current month
var str=new Date().getFullYear()+'';
str= str.match(/\d{2}$/);//current year is 15(as this year is 2015)
var strprev= str-1;//previous year is 14
var dynmonths = new Array();
dynmonths = monthNames.slice(ctdate).concat(monthNames.slice(0, ctdate));
//here the output comes for last 12 months starting from currentmonth-12 (i.e APR in this case) to current month (i.e MAR)
//dynmonths = ["APR","MAY","JUN","JUL","AUG","SEP","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR"];
//I am rotating dynmonths in a for loop to get full dates i.e between (01-APR-14 to 01-MAR-15)
for (var i = 0, length = dynmonths.length; i < length; i++) {
var month = '01-' + dynmonths[i] + '-' + strcurrent;
}
But the problem is that month is taking 14for all the months. Which is wrong. After 01-DEC-14 the next month must be 01-JAN-15, 01-FEB-15 and so on. How to check DEC in for loop and after DEC year must change to year+1
Thanks in advance
use below code it will work.
function ddd()
{
var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var ctdate = (new Date()).getMonth() + 1;// getting current month
var str=new Date().getFullYear()+'';
str= str.match(/\d{2}$/);//current year is 15(as this year is 2015)
var strprev= str-1;//previous year is 14
var dynmonths = new Array();
dynmonths = monthNames.slice(ctdate).concat(monthNames.slice(0, ctdate));
//here the output comes for last 12 months starting from currentmonth-12 (i.e APR in this case) to current month (i.e MAR)
//dynmonths = ["APR","MAY","JUN","JUL","AUG","SEP","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR"];
//I am rotating dynmonths in a for loop to get full dates i.e between (01-APR-14 to 01-MAR-15)
for (var i = 0, length = dynmonths.length; i < length; i++) {
if(dynmonths[i]=='JAN')
{
var str = parseInt(str)+parseInt(1);
}
var month = '01-' + dynmonths[i] + '-' + str;
document.writeln(month);
document.write("<br />");
}
}
<body onload="ddd()">
You can declare variable bool = false and check if you on DEC change it to true (or use counter from more then one year):
var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var ctdate = (new Date()).getMonth() + 1;// getting current month
var str=new Date().getFullYear()+'';
str= str.match(/\d{2}$/);//current year is 15(as this year is 2015)
var strprev= str-1;//previous year is 14
var dynmonths = new Array();
dynmonths = monthNames.slice(ctdate).concat(monthNames.slice(0, ctdate));
//here the output comes for last 12 months starting from currentmonth-12 (i.e APR in this case) to current month (i.e MAR)
//dynmonths = ["APR","MAY","JUN","JUL","AUG","SEP","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR"];
//I am rotating dynmonths in a for loop to get full dates i.e between (01-APR-14 to 01-MAR-15)
var isPassYear = false;
for (var i = 0, length = dynmonths.length; i < length; i++) {
var month;
if (isPassYear)
//do something
else
month = '01-' + dynmonths[i] + '-' + strcurrent;
if (monthNames[11] == dynmonths[i]) {
isPassYear = true;
}
}
second option is to use Date object and append his month by one each time, if you set append to month number 12 it automatic go to the next year.
I need to have an array of dates for whole days of the last week, including the current day, for e.g
['05/06', '04/06', '03/06', '02/06', '01/06', '31/05', '30/05']
(format dd/mm)
how can i do this?
I know there is the Date() object, but other than that I'm stumped.
logic along the lines of:
var dates = [];
var today = new Date();
for (var i = 0; i<7; i++){
var date = today - (i+1);
dates.push(date);
}
So you want an array containing todays date and a further 6 elements, with todays date-1, todays date-2 etc...?
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
var str = tempDate.getDate() + "/" + tempDate.getMonth();
dates.push(str);
}
console.log(dates);
Output: ["5/5", "4/5", "3/5", "2/5", "1/5", "31/4", "30/4"]
If you need numbers with leading 0's, try this:
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
var str = pad(tempDate.getDate()) + "/" + pad(tempDate.getMonth());
dates.push(str);
}
console.log(dates);
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}
Output: ["05/05", "04/05", "03/05", "02/05", "01/05", "31/04", "30/04"]
Check this working sample, where all days are printed out:
http://jsfiddle.net/danyu/Tu5R6/6/
This is the main logic:
for(var i=7;i>0;i--)
{
tempDate.setDate(tempDate.getDate()-1);
output+=tempDate+"<br/>";
}
Modify it to store those days into your array.
I have a string that has a date in it and I wan't to be able to convert it.
var startDate = "March-09-2010";
var convertedStartDate = new Date(startDate);
var month = convertedStartDate.getMonth() + 1
var day = convertedStartDate.getDay();
var year = convertedStartDate.getFullYear();
var shortStartDate = month + "-" + day + "-" + year;
alert(shortStartDate);
I want it so it converts March-09-2010 to 09-03-10 (DD-MM-YY)
Anyone know what I am doing wrong?
var startDate = "March-09-2010";
var convertedStartDate = new Date(startDate.replace(/-/g, "/")); // replace hyphen with slash
var month = convertedStartDate.getMonth() + 1
var date = convertedStartDate.getDate();
var year = convertedStartDate.getFullYear();
var shortStartDate = date + "-" + month + "-" + year;
alert(shortStartDate);
demo: http://jsfiddle.net/BjnBW/
Try this:
var dt=Date.parse(Yourstring);
formatDate('DD-MM-YY',dt);
Please check this Date.parse
Check your syntax changed your code a little, modify it according to it then ---
var startDate = "March/09/2010";
var convertedStartDate = new Date(startDate);
var month = convertedStartDate.getMonth() + 1
var day = convertedStartDate.getDate();
var year = convertedStartDate.getFullYear();
var shortStartDate = day+ "-" + month+ "-" + year;
alert(shortStartDate);
your date string is not in the correct format. for correct formats, please see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
try this or jsfiddle
var startDate = "March-09-2010";
var tmp = startDate.split('-');
tmp.splice(1, 0, ',');
var convertedStartDate = new Date(tmp.join(' '));
var month = convertedStartDate.getMonth() + 1
var day = convertedStartDate.getDate();
var year = convertedStartDate.getFullYear();
var shortStartDate = ('0' + day).slice(-2) + "-" + ('0' + month).slice(-2) + "-" + year;
alert(shortStartDate);
var shortStartDate =
Globalize.format(Globalize.parseDate(startDate, 'MMMM-dd-yyyy'), 'dd-MM-yy');
Use some library to do the conversion, because the built-in Date.parse() is implementation-dependent. It depends on the system locale what formats it accepts.
The code above uses Globalize.js, which can handle a large number of date formats, including formats with month names in different languages (the default being English).
You'll need to convert 'March' to a number. One way is to use this Array extension to be able to retrieve a month number from a month name:
Array.prototype.enum = function(){
var obj = {};
for (var i=0; i<this.length; (i+=1)) {
obj[this[i]] = i;
}
this.enum = obj;
return this;
};
Now, create an Array with month names
var months = ('January,February,March,April,May,June,July,'+
'August,September,October,November,December').split(',')
.enum();
Now you rewrite your date:
var startDate = "March-09-2010".split(/\-/),
month = months.enum[startDate[0]]+1;
startDate = [startDate[1],
month < 10 ? '0'+month : month,
startDate[2]].join('-');
//=> startDate now is: '09-03-2010'
Use getDateFromFormat() to convert string to date in javascript.
Check this link for more help: http://www.mattkruse.com/javascript/date/
I have dates and times in a database in the following format:
2011-08-02T00:00:00-00:00
What is the easiest way to convert them to something like 8-2-2011?
Thanks,
var date = "2011-08-02T00:00:00-00:00".split('T')[0].split('-').reverse();
var month = date[0], day = date[1];
//remove 0 in the beginning if not necessary
if (+month < 10) {
month = month.slice(1);
}
if (+day < 10) {
day = day.slice(1);
}
//swap between the two
date[0] = day;
date[1] = month;
date.join('-');
Or you can use the boring Date way.
Here's the code:
x=new Date("2011-08-02T00:00:00-00:00")
str=(x.getUTCMonth()+1)+"-"+x.getUTCDate()+"-"+x.getUTCFullYear()
Or:
x="2011-08-02T00:00:00-00:00"
x=/^(\d+)\-(\d+)\-(\d+)/.exec(x)
if(x){
str=(parseInt(x[2],10)+"-"+parseInt(x[3],10)+"-"+parseInt(x[1],10))
}
This format will work in the Javascript Date constructor:
var d = new Date("2011-08-02T00:00:00-00:00");
var month = d.getUTCMonth() + 1;
var day = d.getUTCDate();
var year = d.getUTCFullYear();
var output = month + "-" + day + "-" + year;
one way could be to split up the date part
var date = "2011-08-02T00:00:00-00:00";
var dpart = (date.substr(0,10)).split("-");
var odate = parseInt(dpart[1],10)+"-"+parseInt(dpart[2],10)+"-"+dpart[0];