Extract group of characters from an object/string - business-objects

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)

Related

How to get only numbers from a string

I have some code in json to get data from domoticz.
local Data = parsedJson["result"][1]["Data"]
print("Data:", Data)
The outcome;
2.700 kWh
How to get rid of the kWh ?
So the outcome will be 2.700
You can write a regular expression which accepts numbers and . only.
Not sure but you could try to cast the variable to float and it will preserve only the number with floating point

Converting date and time from response JSON in human readable format

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.

Is it possible to write date as DateTime format in JSON using Python without converting it to string

I want to know if there is a way to write the date as DateTime format in JSON.
I have followed so many links on the internet but everywhere date is converted to string(str) in order to write it on JSON file.
I used the below code:
import json
fileName='json_output.json'
def writeToJSONFile(data):
with open(fileName, 'a+') as fp:
json.dump(data, fp, indent=4, default=str)
then calling it as :
from datetime import datetime
date_value="09-23-2019"
date_time = datetime.strptime(date_value,'%m-%d-%Y')
date_dict={"eventDate":date_time}
writeToJSONFile(date_dict)
The above code is able to write date into the JSON file but in the string format.
I have already went through link:
How to overcome "datetime.datetime not JSON serializable"?
JSON datetime between Python and JavaScript
I just want to know if it is possible or not at all possible to store date as datetime format?
Simple answer is No, JSON does not have a native datetime representation, so it will appear as a string; however, if you use a standard that is agreed upon, the receiving application can parse the variable into a datetime object, if they choose to. If you do not know what format they may agree with, I would recommend just making it a standard ISO 8601(Combined date and time in UTC), that way it can be parsed by the receiver, and retain the correct value regardless of time zone.

google app scripts Utilities.FormatDate malfunction

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.

Understanding the Result from openweathermap in Json format

I have to get weather forecast for 7 days of a particular location(with postal code 94042). For this I make a HTTP request like this:
http://api.openweathermap.org/data/2.5/forecast/daily?q=94042&mode=json&units=metric&cnt=7
The result of the query is a long string in Json format. I did not post the result as it is messy, you can click the link to view the result:
There are some parts of the result about which I am confused.
What does the dt field in the beginning of every element of the array list signify?
What does the second last field deg of every element in the array list signify?
What does the last field clouds followed by an integer signify? I thought description about clouds was covered in the main field?
Explanation of the parameters: http://openweathermap.org/weather-data#current
dt: Data receiving time (in unix, UTC format). dt is the time of data receiving in unixtime GMT (greenwich mean time). To convert this value, use a 3rd-party library like momentjs.com or refer to this answer on how to convert it: https://stackoverflow.com/a/847196/3412545
deg: Wind direction (in degrees, meteorological)
clouds: cloudiness also known as cloud cover (in %)