I am trying to create a date object by using a variable obtained from a database. The string is already in the correct format, already comma delimited "yyyy,mm,dd,hh,mm,ss". However trying to create a Date object returns an Invalid Date error.
var foo ='2012,03,09,12,00,00,00';
document.write(foo); //<-- obviously writes the string 2012,03,09,12,00,00,00 to the browser
var then=(new Date(foo));
document.write(then); //<-- returns Invalid Date
I have a solution which is the following:
var x = foo.split(/[,]/);
var then = new Date(x[0], x[1], x[2], x[3], x[4], x[5]);
Wondering why this is needed when essentially it's recreating the same string that was passed to it.
It's because the string you are trying to convert into a Date object is not valid. The Date object doesn't just accept any format as a string. if it is not recognised it wont work.
See Date doc https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Here is information about format supported
https://www.rfc-editor.org/rfc/rfc2822#page-14
"yyyy,mm,dd,hh,mm,ss" is not a "correct format" for a date string.
The JavaScript Date object can only parse specific formats. Check the MDN docs for Date for valid dateStrings. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Anyway. your 2nd example works because you're not recreating the string, you are are passing 6 different parameters compared to one long one.
You can't pass a comma-separated string to a function and expect it to break it into parameters, it doesn't work that way.
Related
I have a User Input which consists of a Date and a Time Input. Both of those values are send as a full date like
reminder_date: '2021-02-15T08:00:00.000Z',
reminder_time: '2021-02-09T17:00:00.000Z'
But i want to store it in my db as one single value like
'2021-02-15T17:00:00.000Z'
what is the best approach to achieve that ? split the strings on T and then combine or is there a simple way i can take the date part and the time part and create a new dateTime to save
If the two items are strings, you could simply use the JavaScript substr function to get the first 11 characters from reminder_date and the second part of reminder_time (starting from 11), then concatenate them, e.g.
let reminder_date = '2021-02-15T08:00:00.000Z';
let reminder_time = '2021-02-09T17:00:00.000Z';
let reminder_date_time = reminder_date.substr(0, 11) + reminder_time.substr(11);
console.log(reminder_date_time);
I am getting 2018-06-10 00:29:04 this type of value in the key date. I just want to display date without time.
I want to have 2018-06-10 from 2018-06-10 00:29:04.
If it's a fixed string that you're working with, as in you know it'll always be in that format, you can just truncate it like so:
var datetimestamp = "2018-06-10 00:29:04";
var dateTruncated = datetimestamp.slice(0, 10);
If it's not fixed, you can split on spaces like #Shubh mentioned in his comment and take the first array value.
This question already has answers here:
ToLocaleDateString() changes in IE11
(5 answers)
Closed 7 years ago.
I have a date that I am converting to LocaleDateString and then splitting it into an array inside an angulrajs controller. When I try to convert to int the elements in the array I get NaN. The characters in the array are numbers but the parse is not working.
How can I parse that data properly?
Code:
var dateLocal = $scope.startDate.toLocaleDateString(); //Has this 6/5/2015
var dateSplitted = dateLocal.split("/"); //Has [6,5,2015]
var month = parseInt(dateSplitted[0]); //HERE If I use parseIntI get NaN originally it has 6
var day = dateSplitted[1];//Has 5
var year = dateSplitted[2]; Has 2015
I want to be aple to convert to string month day and year.
You rely on toLocaleDateString, which is implementation dependent:
This function returns a String value. The contents of the String are
implementation-dependent
The problem is that your browser returns a string with some left-to-right marks (U+200E).
See the difference:
var str1 = "6", // "\u0036" <-- OK
str2 = "6"; // "\u200e\u0036\u200e" <-- Your "corrupted" string
parseInt(str1); // 6
parseInt(str2); // NaN
So you shouldn't trust the value returned by that method. Instead, use date methods to get the day, month and year.
This doesn't answer why you are getting NaN in your code, but instead of converting the date object to a string and parsing the parts, you can get the parts of the date directly using Date.prototype.getMonth(), Date.prototype.getDate() (day-of-month), and Date.prototype.getFullYear().
I tried out your code, and it seemed to work fine for me. For your dateLocal, I just replaced your value with var dateLocal = new Date().toLocaleDateString(); because I didn't know what value was loaded from your scope. When I did this, the code loaded fine, so you may want to double check the nature of the variable you are loading from the scope.
I've generated the following JSON Object in php and am passing it through AJAX to my javascript. This JSON object as is will not suffice since the Google Charts API I am using needs a javascript date object for each date in the JSON.
My dates are currently encoded in UNIX timestamp -- 1199170800,1201849200,1343800800,1346479200. I figure I could add a regex quantifier on one side of the timestamp (ie: ~1346479200), use REGEX to find the date based on the quantifier, convert the date to javascript object, and then replace each quantifier with the regex. Easier said then done for some.
{"cols":[{"type":"date","label":"FromDate"},{"type":"number","label":"Electricity Use
(KWH)"},{"type":"number","label":"Cooling Degree Days"}],"rows":[{"c":
[{"v":1199170800,"f":"Jan-08"},{"v":"559280","f":"559,280"},{"v":"0"}]},{"c":
[{"v":1201849200,"f":"Feb-08"},{"v":"653193","f":"653,193"},{"v":"381"}]},{"c":
[{"v":1343800800,"f":"Aug-12"},{"v":"667874","f":"667,874"},{"v":"322"}]},{"c":
[{"v":1346479200,"f":"Sep-12"},{"v":"687299","f":"687,299"},{"v":"101"}]}]}
I've looked through many similar posts to get some ideas, but I can't solve this one... Similar, seemingly useful posts:
Loop through object get value using regex key matches Javascript
http://sudarshanbhalerao.wordpress.com/2011/08/14/convert-json-date-into-javascript-date/
Date Range Google Chart Tools
No regular expressions needed.
Parse the JSON into a JavaScript object.
Loop through the "rows" key of the resulting object.
For each item, get the "c" key, which looks like it's the value you need.
In the above example, the values are strings, so you should convert them to integers using parseInt.
Create a new Date object with each one, and pass it the integer value, multiplied by 1000. If you do new Date(integer), it will create an object using that timestamp, but the value is expected to be in milliseconds, not seconds like a normal UNIX timestamp.
Here's the breakdown:
What code do I need to extract the 12, 26, and 48 from a field whose value is 101219488926 and then display it in the format MM/DD/YY? (So in this case, the new value would need to be 12/26/48)
Here's the long version:
I'm using a mag-stripe reader that takes information from the swiped card (a driver license) and then uses that info to auto-populate certain fields in a PDF (first name, last name, date of birth, etc).
Everything works fine, with one exception: the date of birth. Even that does technically work, but the value is in this format (assuming the person's DOB is 26 December 1948):
101219488926
What I need is: the month (12), day (26), and year (1948) stripped out of that long number, then converted to display in the format MM/DD/YY
Outside of Acrobat, this seems to work just fine:
var dob = 101219488926;
trimmonth = dob.substring(2,4);
trimday = dob.substring(10,12);
trimyear = dob.substring(6,8);
dob.value = trimmonth + "/" + trimday + "/" + trimyear;
Any suggestions?
The code you have there shouldn't work - substring is a String function, so you would need to convert that number you have to a string for it to be available. Setting dob.value is also suspect, since dob is a Number, and numbers do not have a value property.
Of course, it's obvious that you're not showing the actual code you have tried, but something like this would probably work:
// Appending blank string to type coerce
var dob = 101219488926 + '';
// Array.join to glue the numbers together
// (no reason why you **have** to use this; the original method will work fine too)
dopInput.value = [dob.substring(2,4), dob.substring(10,12), dob.substring(6,8)].join('/');