In need to obtain a date in a integer format ddmmyyyy using as argument a cell that contains a date in standard google spreadsheet format.
That's the code.
function getDateToInt(date) {
ddmmyyyy = Utilities.formatDate(date,SpreadsheetApp.getActive().getSpreadsheetLocale(),'dd/MM/yyyy');
var array = new Array();
array = (ddmmyyyy.split('/'));
return (parseInt(array[2])*100+parseInt(array[1]))*100+parseInt(array[0]);
}
Here the problem:
The function behave as expected in all case except when the month in the argument date is 8 or 9. In these cases returns #NUM!.
Any contribution is welcome.
If you are interested in using just a formula, the formula
=value(A1)
where a1 is a standard date such as 1/1/2016 with return "42370"
the number integer of the date.
parseInt() uses 2 arguments, the variable to parse and the base.
The base is frequently omitted and assumed to be 10 but this is actually not ideal. Read the doc here.
I guess that adding the base parameter will solve the issue you have but you can alternatively use the Number() function which will also convert the string into a number.
Related
I am working on an Azure Logic App that is triggered via an HTTP call and returns a JSON response. Internally, the logic app retrieves JSON data from a web API and then converts the response JSON data to a format that is acceptable to the calling client of the logic app.
The problem I'm having is that the web API is returning dates in the format "/Date(1616371200000)/" and I need the date format to look like "2021-03-32T19:00:00Z". I don't see any built-in logic app function that can work with or convert the Epoch timestamp as-is (unless I'm missing something).
To clarify...
Source Data:
{
"some_silly_date": "/Date(1616371200000)/"
}
Desired Data:
{
"some_silly_date": "2021-03-32T19:00:00Z"
}
The following solution would theoretically work if the source date wasn't wrapped with "/Date(...)/":
"#addToTime('1970-01-01T00:00:00Z', 1616371200000, 'Second')"
Parsing that text off the timestamp before converting it would lead to a really ugly expression. Is there a better way to convert the timestamp that I'm not aware of?
Note that using the Liquid JSON-to-JSON templates is not an option. I was using that and found this action apparently has to JIT compile before use which is causing my logic app to time-out when being called after a long period of inactivity.
Can you get the value "/Date(1616371200000)/" from the JSON into a variable? If so, a little string manipulation would do the trick:
int(replace(replace(variables('data_in'),'/Date(',''),')/',''))
Then use the variable in the addToTime function.
Result:
The following expression seems to be working and returns a timestamp in UTC. Note that the substring() function is only using a length of 10 instead of 13. I'm intentionally trimming-off the milliseconds from the Epoch timestamp because the addToTime() function only handles seconds.
{
"some_silly_date": "#addToTime('1970-01-01T00:00:00Z', int(substring(item()?['some_silly_date'],6,10)), 'Second')"
}
As an added bonus, if the timestamp value is null in the source JSON, do the following:
{
"some_silly_date": "#if(equals(item()?['some_silly_date'], null), null, addToTime('1970-01-01T00:00:00Z', int(substring(item()?['some_silly_date'],6,10)), 'Second'))"
}
Quite possibly the ugliest code I've ever written.
the following function receives an event from the Angular Material Design DatePicker and shall print it to the console:
applyFilter(event: MatDatepickerInputEvent<Date>) {
console.log(`${event.value}`); // prints the selected date
console.log(event.value); // prints some weird object
}
But they are each printing a different output to the console.
The first prints the actual selected date, the second one prints some weird object to the console.
This is the console output:
This is my DatePicker:
<mat-form-field>
<mat-label>Anfangsdatum</mat-label>
<input matInput (dateInput) = "applyFilter($event)" [matDatepicker] = "startDatePicker">
<mat-datepicker-toggle matSuffix [for]="startDatePicker"></mat-datepicker-toggle>
<mat-datepicker #startDatePicker></mat-datepicker>
</mat-form-field>
I just want to get the date the user picked using the DatePicker from Angular Material Design, but it seems like that this overly complicated. Can someone please help me?
You are seeing two different values.
When you call console.log(new Date()) you will get the actual date object. (replace new Date() with any date object such as your event.value)
This object is the weird object that you speak of. It's simply a Date which has all kinds of methods available to help you. But in the end, all a Date is actually is a Number that represents milliseconds since 1 January 1970 UTC, then a bunch of helper functions with it.
One of those helper functions is .toString().
When you call console.log(`${new Date}`); you are creating a date object, then putting it inside a template literal (the backticks). This template literal calls the .toString() of the date object to convert it to a string, as you are asking for a string with a template literal. So you get the string representation of the Date object. (It's actually a bit more complex than that per this post, it's not always the toString() method per this post).
So if you want the string value, use the template literal or just call event.value.toString(). If you want the date object so you can do things with it (use the full date object and all its abilities per the documentation linked at the top)... use the date object. If you have other questions, feel free to comment.
Edit:
I don't think it's the actual date object. Check out the API. You are getting the object you are expecting, but it may be the one described in this API. I noticed they both have the add method in the prototype. Either way, it's an object vs the string representation. I tried to find the type definition since I'm assuming you are in Typescript, that should list all the methods available to you.
The date and time in a response JSON looks like this:
2019-02-17T05:28:00
I tried to convert it with Regex to the following format:
17.02.2019 at 05:28:00
But failed.
let string = string
.split("T")
.join("")
.split("-")
.join("");
string = string.replace(
/(\d{4})(\d{2})(\d{2})(\d{8})/,
"$3$2$1$4"
);
What is the right way?
You can capture year month date and time in different groups using this regex,
(\d{4})-(\d{2})-(\d{2})T(\d{2}:\d{2}:\d{2})
And replace it with this,
$3.$2.$1 at $4
Demo
Javascript demo,
var s = "2019-02-17T05:28:00"
console.log(s.replace(/(\d{4})-(\d{2})-(\d{2})T(\d{2}:\d{2}:\d{2})/g,'$3.$2.$1 at $4'))
Your best bet is probably to use a date-time handling library such as Moment.js, which provides all sorts of nice date manipulation functionality.
You can then do things like
const moment = require("moment");
let formattedDate = moment.utc(isoDateVariable, moment.ISO_8601).format("DD/MM/YYYY");
It also helps you with all the general nastiness associated with dates and times in programs which you really don't want to be handling yourself.
I have a very long string of characters that contain a date and time. How can I extract just the date part and the time part from it?
Here's the string:
0:2019010309130000:0.000000:126:0
I just need to extract the date and the time as in:
Date - 20190103
Time - 0913 and format it as 09:13
This applies to Python programming language.:
If the string stays in the same format I believe you should be able to extract it using a concept known as "slicing" (you can find more about it in here).
x = "0:2019010309130000:0.000000:126:0"
y = 'Date - {} Time - {}:{}'.format(x[2:10], x[10:12], x[12:14])
print(y)
I'm trying to understand a simple basic concept regarding JSON strings. I'm running a simple test that looks like this:
$(document).ready(function() {
var last = 9;
var json1 = $.parseJSON('{"id":"10"}');
var json2 = $.parseJSON('{"id":10}');
if(json1.id > last)
alert("json1.id is greater than last");
if(json2.id > last)
alert("json2.id is greater than last");
});
Since the variable "last" is type int I'm trying to make a comparison between it and the "id" from two different JSON strings. json1 denotes the ten value as a string, whereas json2 denotes it as an integer value. When this is run, both alerts are executed. I did not expect that. I expected that the second alert would execute, but not the first one since ten is presented as a string.
I believe that the correct way to format an integer value in JSON is in json2, right?
Why is the first test executing the alert?
I'm trying to troubleshoot a larger project and thought the problem might be in the way the JSON string is formatted.
The documentation of Javascript's operators holds all the answers:
Strings are compared based on standard lexicographical ordering, using
Unicode values. In most cases, if the two operands are not of the same
type, JavaScript attempts to convert them to an appropriate type for
the comparison. This behavior generally results in comparing the
operands numerically. The sole exceptions to type conversion within
comparisons involve the === and !== operators, which perform strict
equality and inequality comparisons. These operators do not attempt to
convert the operands to compatible types before checking equality.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Comparison_operators