Date extraction JavaScript for Acrobat does not work - javascript

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('/');

Related

How to combine Date and Time from 2 different var's in NodeJS

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

How use a NumberFormat function on google apps script

I have a webapp that I get a value. I make an append row with this data for my sheet. However, I want that this data have a text format, because I want to get the value like I typed.
Some problems happen with my sheet, like an "and" be recognized as a number.
Ex: 2000e7 appears as: 2.00E + 10.
I know that yhave a function to change the format. NumberFormat (" ")
But I think that I'm not using it correctly.
My code:
var ws= ss.getSheetByName(flag);
ws.appendRow([Entrada,Modelo,SN,Ocorrencia,Status,data,obs])
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss2.getSheets()[0];
var range = sheet.getRange("My Last row");
// Money format
range.setNumberFormat("#");
Sometimes the better way is to prepend the value with ' rather than using setNumberFormat
Related
Script 'setValues' method interprets strings as numbers in cells

How to convert date format coming in an ajax object?

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.

Javascript String to Date works in Chrome, not FF or Safari

I have an element that shows a date string in this format:
<div> ListDate: 2014-11-22 00:00:00.0 </div>
I have a variable that captures that text, now I am stripping out all but the date string and trying to convert it to a date so I can do some math on it. My code works fine in Chrome, but always returns NaN in FF and Safari. Here is the javascript:
var listDate = " ListDate: 2014-11-22 00:00:00.0 ";
listDate = listDate.replace('ListDate:', '');
listDate = $.trim(listDate);
listDate = Date.parse(listDate);
I'm watching it in Firebug, and it performs as expected up until Date.parse(). I tried the solutions shown on this thread, but still no luck. Any ideas? I don't have control over the html, it comes from a web service.
The full ISO8601 format (IIRC) is expecting a capital letter "T" in between the date and time portions of that string, e.g. ""2014-11-22T00:00:00.0"" https://en.wikipedia.org/wiki/ISO_8601 (I'd therefore recommend always adding the T delimiter.
E.g. the following code will parse to NaN in some browsers:
var myAlmostISODate = "2014-11-22 00:00:00.0";
console.log("Date: " + Date.parse(myAlmostISODate));
//IE11: NaN
//Firefox: 1416632400000
//Chrome: 1416632400000

JavaScript Date object not working with string passed to it

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.

Categories

Resources