Is anyone can help for splitting the strings into JSON?
Below is few examples:
"name":"#MATRIX COPIL 33 agenda Sun Nov 20 2022 18:00:00 GMT-0500 (Eastern Standard Time)"
"name":"#CQI2022 COPIL 8 Tue Nov 22 2022 00:00:00 GMT+0100 (Central European Standard Time)"
"name":"#GED2023 Copil 10 Mon Nov 21 2022 18:00:00 GMT-0500 (Eastern Standard Time)"
Into Json objects like below
"project_name": "#GED"
"purpose": "COPIL"
"copil_number": "10"
"dateofcreation": "Sun Nov 20 2022 18:00:00 GMT-0500"
Also if it's possible to get the week number for Json object based on dateofcreation ?
"week_file": "47"
I have the (german) Output: Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)
and I would like to have just the yyyy-mm-dd date: 2022-09-09
Thank you for the help!
Date's are not too great in JS, the reason that libs like moment.js etc are very popular.
But one idea is to simply replace the GMT+0300 to GMT+0000, and then parse into Date object and use ToISOString and slice the first 10 chars.
eg.
function shortDate(dt) {
return new Date(dt.replace(/GMT\+..../,'GMT+0000')).toISOString().slice(0, 10);
}
console.log(shortDate('Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)'));
Below is an example of what I'm trying to clean, I want only the prices but without the $ in front of it because I am going to average the prices.
$11.78 | Tue Sep 07 2021 14:57:34 GMT-0500 (Central Daylight Time)
$10.63 | Tue Sep 07 2021 14:58:19 GMT-0500 (Central Daylight Time)
$9.69 | Tue Sep 07 2021 14:59:20 GMT-0500 (Central Daylight Time)
$9.68 | Tue Sep 07 2021 14:59:35 GMT-0500 (Central Daylight Time)
$11.21 | Tue Sep 07 2021 15:00:36 GMT-0500 (Central Daylight Time)
$9.51 | Tue Sep 07 2021 15:01:07 GMT-0500 (Central Daylight Time)
I am currently trying to use the below code to clean everything but what I want as said above it almost gives me what I want but, not close enough.
// the example of what im trying to clean above is a string called data
var data_array = Array.from(data)
var data = data_array.join("")
var data = data.split("$")
var data = data.join("")
var data = data.split("|")
var data = data.join("")
If I understand you correctly:
let data = '$11.78 | Tue Sep 07 2021 14:57:34 GMT-0500 (Central Daylight Time) $10.63 | Tue Sep 07 2021 14:58:19 GMT-0500 (Central Daylight Time)$9.69 | Tue Sep 07 2021 14:59:20 GMT-0500 (Central Daylight Time) $9.68 | Tue Sep 07 2021 14:59:35 GMT-0500 (Central Daylight Time)$11.21 | Tue Sep 07 2021 15:00:36 GMT-0500 (Central Daylight Time) $9.51 | Tue Sep 07 2021 15:01:07 GMT-0500 (Central Daylight Time)'
let data_array = data.split("$").map(a => a.slice(0, a.indexOf('|')))
data_array.shift()
console.log(data_array)
The easiest way is regex. Here, I am matching any non whitespace character between $ and |.
const data = "$11.78 | Tue Sep 07 2021 14:57:34 GMT-0500 (Central Daylight Time) $10.63 | Tue Sep 07 2021 14:58:19 GMT-0500 (Central Daylight Time) $9.69 | Tue Sep 07 2021 14:59:20 GMT-0500 (Central Daylight Time) $9.68 | Tue Sep 07 2021 14:59:35 GMT-0500 (Central Daylight Time) $11.21 | Tue Sep 07 2021 15:00:36 GMT-0500 (Central Daylight Time) $9.51 | Tue Sep 07 2021 15:01:07 GMT-0500 (Central Daylight Time)"
const prices = data.match(/(?<=\$)\S*(?=|)/gm);
console.log(prices)
Quick tutorial on Regex
Regex uses a pattern to match certain characters in a string.
Some basic concepts you should know are:
groups: (), A group of characters, or conditions, you want to match.
A simple usecase would be matching a word, such as (title).
Groups also have a special feature where you can do checks before and after the group. These checks start with a question mark like (?=title).
These checks are:
(?=) positive look ahead
(?!) negative look ahead
(?<=) positive look behind
(?<!) negative look behind
These checks are useful for what we want to do, which is match between two character, $ and |.
Now, how would we use these checks?
For the example, we will use a part of your string.
$11.78 | Tue Sep 07 2021 14:57:34 GMT-0500 (Central Daylight Time)
Now, for this example, we will try and get the text in the brackets Central Daylight Time.
We want to make sure that our match has a bracket before and after it. Since we can't to check before, we want to be positive that the bracket is behind. So we will use positive look behind (?<=).
And we want to check for a open bracket (, so we add it after. (?<=\(). We use \ to escape the bracket, since it's a special character. You can see I also did so in the answer for (?<=\$).
Now we need to make sure that the character after, is a closed bracket ).
We do the same, but this time we use a positive look ahead instead (?=\)).
Now we want to match all the word characters between them. So in between both groups we add the quantifiers. In this case .+
Now to teach you about quantifiers. Quantifiers tell you how many times to repeat the match. Here are the basic quantifiers.
* 0 or more
? 0 or 1
+ 1 or more
{n} n times
{n, } n or more times
{n, n2} between n and n2
Out of these quantifiers, the most common ones you will use are * and +.
Now we will look at character groups.
\w any word character.
\W any non-word character.
\d any digit.
\D any non digit.
Then there is the wild card . that matches anything. We use it in the example, just because it's the cleanest, but if you look at my answer, I use \S that stand for any non whitespace character, which I used, so it wouldn't match the space after the price. There are many character groups, and you can google to find more.
Now we can add all the parts together, to get our Reg'ex pattern.
(?<=\().+(?=\))
Now I will wrap our pattern in // so we can add extra features. In this case, global - g, and multi-line - m. There are other settings you can use too, such as case-insensitive - i, but we don't need it for ours So now we have:
/(?<=\().+(?=\))/gm
Now we can try it in some JS.
const data = "$11.78 | Tue Sep 07 2021 14:57:34 GMT-0500 (Central Daylight Time)";
const result = data.match(/(?<=\().+(?=\))/gm);
console.log(result);
Any more questions, ask in the comments, and I'll respond. Also, if you want to test your reg'ex, you can use Regex101. It has a useful menu at the bottom right to read through the different features of Reg'ex.
Try this approach:
What you need is to split data into rows with splitting by a new line.
from there its easier to map over each row splitting it with separator '|' and splitting each price on two parts "$" and actual price, where yo pick the part you need which is numeric price representation.
const data =
`$11.78 | Tue Sep 07 2021 14:57:34 GMT-0500 (Central Daylight Time)
$10.63 | Tue Sep 07 2021 14:58:19 GMT-0500 (Central Daylight Time)
$9.69 | Tue Sep 07 2021 14:59:20 GMT-0500 (Central Daylight Time)
$9.68 | Tue Sep 07 2021 14:59:35 GMT-0500 (Central Daylight Time)
$11.21 | Tue Sep 07 2021 15:00:36 GMT-0500 (Central Daylight Time)
$9.51 | Tue Sep 07 2021 15:01:07 GMT-0500 (Central Daylight Time)`;
const rows = data.split('\n');
const prices = rows.map(el => el.split(' | ')[0]).map(p => p.split('$')[1]).filter(e => e !== undefined);
console.log(prices);
Here is regex approach:
Regex: (([$]\d{1,3}[$,\\.]?(\d{1,3})?))
const regex = /(([$]\d{1,3}[$,\\.]?(\d{1,3})?))/gm;
const str = `\$11.78 | Tue Sep 07 2021 14:57:34 GMT-0500 (Central Daylight Time)
\$10.63 | Tue Sep 07 2021 14:58:19 GMT-0500 (Central Daylight Time)
\$9.69 | Tue Sep 07 2021 14:59:20 GMT-0500 (Central Daylight Time)
\$9.68 | Tue Sep 07 2021 14:59:35 GMT-0500 (Central Daylight Time)
\$11.21 | Tue Sep 07 2021 15:00:36 GMT-0500 (Central Daylight Time)
\$9.51 | Tue Sep 07 2021 15:01:07 GMT-0500 (Central Daylight Time)`;
const prices = str.match(regex);
console.log(prices);
I have a variable dateSubmitted having the value dateSubmitted": "07-09-20:11:03:30
I am using Angular Version 7
I am using the date filter within HTML to format the date as below:
<td> {{element.dateSubmitted | date:'MMMM d, y'}} </td>
The date value is correct but the year is incorrectly showing as 2001 in the output July 9, 2001
The correct output is July 9, 2020
See DatePipe input value
value – The date expression:
a Date object
a number (milliseconds since UTC epoch)
an ISO string (https://www.w3.org/TR/NOTE-datetime).
Your date does not conform to ISO string specification, in particular, it has year at the end. The parser seems to interpret is as an hour.
Compare:
new Date('07-09:11:03:30')
// Sun Jul 01 2001 09:11:03 GMT+0200 (Central European Summer Time)
new Date('07-09-:11:03:30')
// Mon Jul 09 2001 11:03:30 GMT+0200 (Central European Summer Time)
new Date('07-09-20:11:03:30')
// Mon Jul 09 2001 20:11:03 GMT+0200 (Central European Summer Time)
Why are the two expressions below returning different results?
Date().valueOf()
"Fri Feb 07 2014 16:03:34 GMT-0500 (Eastern Standard Time)"
new Date().valueOf()
1391807020802
Date().toString()
"Fri Feb 07 2014 16:09:21 GMT-0500 (Eastern Standard Time)"
new Date().toString()
"Fri Feb 07 2014 16:09:26 GMT-0500 (Eastern Standard Time)"
Date() returns a timestamp formatted as a string.
new Date() returns a Date instance.
Instances of the Date constructor have values that convert to numbers, which is why new Date().valueOf() returns a number. Strings are simply strings, so when you call Date().valueOf() you get the same string result.