I am ploting a chart using google annotated timeline chart
https://code.google.com/apis/ajax/playground/?type=visualization#annotated_time_line
I am getting my data through ajax call in json format in an array like this
d[1][0] = 9/30/04, d[2][0] = 12/31/04
i have to loop through these values to add to data rows
for (m = 1; m < datavalues.length; m++) {
data.addRows('DATE', parseFloat(datavalues[m][2]), parseFloat(datavalues[m][3]), parseFloat(datavalues[m][4]), parseFloat(datavalues[m][5])]);
}
But code accepts format like
[new Date(2008, 1 ,1), 30000, null, null, 40645, null, null]
So how to change the format?
If you start with date in format 9/30/04, which seems something like mm/dd/yy, then all you need is to separate those values with split function
var inputDate = '9/30/04',
splitDate = inputDate.split('/'); // => ["9", "30", "04"]
After that, you need to prepare year, month and day numbers
var inputDate = '9/30/04',
splitDate = inputDate.split('/'),
year = parseInt(splitDate[2]) + (parseInt(splitDate[2]) < 50 ? 2000 : 1900), // careful with this, I don't know what years you are dealing with
month = parseInt(splitDate[0]),
day = parseInt(splitDate[1]);
And use them as parameters to create new Date object
var inputDate = '9/30/04',
splitDate = inputDate.split('/'),
year = parseInt(splitDate[2]) + (parseInt(splitDate[2]) < 50 ? 2000 : 1900),
month = parseInt(splitDate[0]),
day = parseInt(splitDate[1]),
date = new Date(year, month, day); // => Date {Sat Oct 30 2004 00:00:00 GMT+0300 (FLE Standard Time)}
You just need to make sure, that format remains the same mm/dd/yy. I would make some sort of date conversion function, that takes input date in specific format and returns date object.
Use this date object in your chart options.
Related
Please anyone share the code to subtract month from this format date(2020,7,24, 11,0). For example,current date with time is (2020,7,24, 11,0) and i get (2020,6,24, 11,0).can anybody help me how to do?
If you have object date, it's simple:
const d = new Date(2020,7,24, 11,0);
// Remember in Date months starts from 0
console.log(d);
// Substract 1 month
d.setMonth(d.getMonth() - 1);
// Log
console.log(d);
Or if you need, you can parse manually
// Set raw
const rawDate = '2020,7,24, 11,0';
console.log(rawDate);
// Convert your date
const [year, month, day, hour, minute] = rawDate.split(',');
// Set date
var d = new Date();
d.setFullYear(year);
d.setMonth(month-1); // Month starts from 0
d.setDate(day);
d.setHours(hour);
d.setMinutes(minute);
console.log(d);
// Substract month
d.setMonth(d.getMonth() - 1);
// Log
console.log(d);
// If you need to convert back to your format
const rawDateModified = `${d.getFullYear()},${d.getMonth()+1},${d.getDate()}, ${d.getHours()},${d.getMinutes()}`;
console.log(rawDateModified);
Updated answer to show how you can pass string to Date object
// You get ds Date param as string
let ds = "2020,2,28,3,44";
// To pass it in Date object, you must
// convert it to array by splitting string
ds = ds.split(',');
// Then pass array to Date object with
// spread operator
const d = new Date(...ds);
// Log
console.log(d);
In Javascript Date objects months are starting from 0 and going to 11 instead of regular 1 to 12 months that we are used to.
In order to format it in a better way, you can use plain Javascript.
let now = new Date();
console.log(now); //Sat Jul 25 2020 13:55:30 GMT+0200 (Central European Summer Time)
let normalValues =[now.getFullYear(),now.getMonth()+1,now.getDate(),now.getHours(),now.getMinutes()];
let formated = normalValues.join(",");
console.log(formated); //2020,7,25,13,55
//Explanation for current time 2020/7/25 14:05
now.getFullYear(); //returns 2020
now.getMonth(); //Returns 6, that is why we add one to it to make it 7
now.getDate(); //Returns 25
now.getHours(); //Returns 14
now.getMinutes(); //Returns 5
I am working with Adobe Acrobat XI Pro. I am using three text boxes for the date with the code below:
var month = this.getField("DATE_MONTH").valueAsString;
var day = this.getField("DATE_DAY").valueAsString;
var year = this.getField("DATE_YEAR").valueAsString;
var date = month + "/" + day + "/" + year;
var test_date = new Date();
test_date.setDate(test_date.getDate());
if(date != test_date){
app.alert("The entered value needs to be TODAY'S date in the format mm/dd/yyyy");
}
Originally, this code was working-- only throwing an error if the date chosen was not today's date. Now I get an error no matter what date is chosen.
Variable date is a string in the format m/d/y, but test_date is a Date object. The != comparison will force the Date to be converted to a string, now the comparison is effectively:
date != test_date.toString();
Since the next version of ECMA-262 requires Date.prototype.toString to return a string in RFC 2822 format (e.g. Mon, 25 Dec 1995 13:30:00 GMT), most new browsers are implementing the format now. The RFC format does not match a string in the format m/d/y so the comparison will always evaluate to true,
E.g.
'12/25/1995' != 'Mon, 25 Dec 1995 13:30:00 GMT'
will always be true for any date when using those formats. For an answer, see Compare two dates with JavaScript.
Just to help out, I don't know anything about ECMAScript in PDFs, but you may be able to do:
var month = this.getField("DATE_MONTH").valueAsString;
var day = this.getField("DATE_DAY").valueAsString;
var year = this.getField("DATE_YEAR").valueAsString;
// Compare date time values with time set to 00:00:00
if (+new Date(year, month - 1, day) != new Date().setHours(0,0,0,0))
The unary + will convert the Date to a number, and the return value from setHours is the new time value (also a number) so both values will be coerced to number. If you want something more explicit, consider:
if (new Date(year, month - 1, day).getTime() != new Date().setHours(0,0,0,0))
var d = new Date();
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
// Is today != today?
console.log( +new Date(year, month - 1, day) != new Date().setHours(0,0,0,0));
// Is today != tomorrow?
console.log( new Date(year, month - 1, day + 1).getTime() != new Date().setHours(0,0,0,0));
PS
This: test_date.setDate(test_date.getDate()) does nothing useful. It just sets test_date's date to the same value that it already has. ;-)
I have this date : 2014071109080706ICT
I need to convert it to Date object in JS
I tried to create new object new Date("2014071109080706ICT") but I get error Invalid date
I also tried cut date string to "20140711090807" and create new Date object but it always generate error : Invalid date
How can i do it ?
You can try to use moment.js .
http://momentjs.com
There are some examples in docs page. One of them is:
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z");
http://momentjs.com/docs/#/parsing/
You can try:
moment("20140711090807+0600", "YYYYMMDDHHmmssZZ");
I think "06ICT" is the timezone info.
You just need to slice the string for each segment and create the date object based on those parts.
var str = "20140711090807";
var year = str.substring(0, 4);
var month = str.substring(4, 6);
var day = str.substring(6, 8);
var hour = str.substring(8, 10);
var minute = str.substring(10, 12);
var second = str.substring(12, 14);
var date = new Date(year, month-1, day, hour, minute, second);
PS: month index is between 0 and 11 so you need to subtract it by 1.
var dateStart = $('input[id=orderdate-0]').val();
var timeStart = $('input[id=ordertime-0]').val();
var dateEnd = $('input[id=orderdate-1]').val();
var timeEnd = $('input[id=orderime-1]').val();
var startDate = new Date(dateStart + " " + timeStart);
var endDate = new Date(dateEnd + " " + timeEnd);
startDate.getTime();
alert(startDate);
i am trying to combine dateStart which is '2013-12-11' and timeStart which is '11:00' and trying to generate date out of it. But i get alert like invalid date. Is there anything wrong in code.?
The Date constructor is very particular about the date string formats it accepts.
Examples:
Dec 25, 1995
Wed, 09 Aug 1995 00:00:00
2011-10-10T14:48:00 (JavaScript 1.8.5+)
There is another constructor that take in the individual components of the date. If you break up your date strings into components you can use the following:
new Date(year, month, day, hour, minute);
Use moment JS plugin http://momentjs.com/
It's a date library for parsing, validating, manipulating, and formatting dates.
That is not a valid format for the parse function, you can use this instead:
var arr = dateStart.split('-');
var timeArr = timeStart.split(':');
new Date(arr[0], arr[1] -1, arr[2], timeArr[0] -1, timeArr[1] -1);
Read this.
Live DEMO
i need to concatenate a date value and a time value to make one value representing a datetime in javascript.
thanks,
daniel
Working with strings is fun and all, but let's suppose you have two datetimes and don't like relying on strings.
function combineDateWithTime(d, t)
{
return new Date(
d.getFullYear(),
d.getMonth(),
d.getDate(),
t.getHours(),
t.getMinutes(),
t.getSeconds(),
t.getMilliseconds()
);
}
Test:
var taxDay = new Date(2016, 3, 15); // months are 0-indexed but years and dates aren't.
var clockout = new Date(0001, 0, 1, 17);
var timeToDoTaxes = combineDateWithTime(taxDay, clockout);
// yields: Fri Apr 15 2016 17:00:00 GMT-0700 (Pacific Daylight Time)
I could not make the accepted answer work so used moment.js
date = moment(selected_date + ' ' + selected_time, "YYYY-MM-DD HH:mm");
date._i "11-06-2014 13:30"
Assuming "date" is the date string and "time" is the time string:
// create Date object from valid string inputs
var datetime = new Date(date+' '+time);
// format the output
var month = datetime.getMonth()+1;
var day = datetime.getDate();
var year = datetime.getFullYear();
var hour = this.getHours();
if (hour < 10)
hour = "0"+hour;
var min = this.getMinutes();
if (min < 10)
min = "0"+min;
var sec = this.getSeconds();
if (sec < 10)
sec = "0"+sec;
// put it all togeter
var dateTimeString = month+'/'+day+'/'+year+' '+hour+':'+min+':'+sec;
Depending on the type of the original date and time value there are some different ways to approach this.
A Date object (which has both date and time) may be created in a number of ways.
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(1995,11,17);
birthday = new Date(1995,11,17,3,24,0);
If the original date and time also is objects of type Date, you may use getHours(), getMinutes(), and so on to extract the desired values.
For more information, see Mozilla Developer Center for the Date object.
If you provide more detailed information in your question I may edit the answer to be more specific.