Date Format JavaScript in a large array - javascript

[Sun Sep 08 2019 05:30:00 GMT+0530 (India Standard Time), Sun Sep 15 2019 05:30:00 GMT+0530 (India Standard Time), Sun Sep 22 2019 05:30:00 GMT+0530 (India Standard Time), Sun Sep 29 2019 05:30:00 GMT+0530 (India Standard Time), Sun Oct 06 2019 05:30:00 GMT+0530 (India Standard Time), Sun Oct 13 2019 05:30:00 GMT+0530 (India Standard Time), Sun Oct 20 2019 05:30:00 GMT+0530 (India Standard Time), Sun Oct 27 2019 05:30:00 GMT+0530 (India Standard Time), Sun Nov 03 2019 05:30:00 GMT+0530 (India Standard Time), Sun Nov 10 2019 05:30:00 GMT+0530 (India Standard Time), Sun Nov 17 2019 05:30:00 GMT+0530 (India Standard Time), Sun Nov 24 2019 05:30:00 GMT+0530 (India Standard Time), Sun Dec 01 2019 05:30:00 GMT+0530 (India Standard Time), Sun Dec 08 2019 05:30:00 GMT+0530 (India Standard Time), Sun Dec 15 2019 05:30:00 GMT+0530 (India Standard Time), Sun Dec 22 2019 05:30:00 GMT+0530 (India Standard Time), Sun Dec 29 2019 05:30:00 GMT+0530 (India Standard Time), Sun Jan 05 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jan 12 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jan 19 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jan 26 2020 05:30:00 GMT+0530 (India Standard Time), Sun Feb 02 2020 05:30:00 GMT+0530 (India Standard Time), Sun Feb 09 2020 05:30:00 GMT+0530 (India Standard Time), Sun Feb 16 2020 05:30:00 GMT+0530 (India Standard Time), Sun Feb 23 2020 05:30:00 GMT+0530 (India Standard Time), Sun Mar 01 2020 05:30:00 GMT+0530 (India Standard Time), Sun Mar 08 2020 05:30:00 GMT+0530 (India Standard Time), Sun Mar 15 2020 05:30:00 GMT+0530 (India Standard Time), Sun Mar 22 2020 05:30:00 GMT+0530 (India Standard Time), Sun Mar 29 2020 05:30:00 GMT+0530 (India Standard Time), Sun Apr 05 2020 05:30:00 GMT+0530 (India Standard Time), Sun Apr 12 2020 05:30:00 GMT+0530 (India Standard Time), Sun Apr 19 2020 05:30:00 GMT+0530 (India Standard Time), Sun Apr 26 2020 05:30:00 GMT+0530 (India Standard Time), Sun May 03 2020 05:30:00 GMT+0530 (India Standard Time), Sun May 10 2020 05:30:00 GMT+0530 (India Standard Time), Sun May 17 2020 05:30:00 GMT+0530 (India Standard Time), Sun May 24 2020 05:30:00 GMT+0530 (India Standard Time), Sun May 31 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jun 07 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jun 14 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jun 21 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jun 28 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jul 05 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jul 12 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jul 19 2020 05:30:00 GMT+0530 (India Standard Time), Sun Jul 26 2020 05:30:00 GMT+0530 (India Standard Time), Sun Aug 02 2020 05:30:00 GMT+0530 (India Standard Time), Sun Aug 09 2020 05:30:00 GMT+0530 (India Standard Time), Sun Aug 16 2020 05:30:00 GMT+0530 (India Standard Time), Sun Aug 23 2020 05:30:00 GMT+0530 (India Standard Time), Sun Aug 30 2020 05:30:00 GMT+0530 (India Standard Time)]
in this, I need dd/mm/yy format JavaScript

x.map(val=>{ return (val.getMonth() + 1) + "-" + val.getDate() + "-" + val.getFullYear()})

You can use .toLocaleDateString("en-US"):
const formattedDates = dates.map(date => date.toLocateDateString("en-US"))
For the provided string of dates, it returns this:
["9/8/2019", "9/15/2019", "9/22/2019", "9/29/2019", "10/6/2019", "10/13/2019", "10/20/2019", "10/27/2019", "11/3/2019", "11/10/2019", "11/17/2019", "11/24/2019", "12/1/2019", "12/8/2019", "12/15/2019", "12/22/2019", "12/29/2019", "1/5/2020", "1/12/2020", "1/19/2020", "1/26/2020", "2/2/2020", "2/9/2020", "2/16/2020", "2/23/2020", "3/1/2020", "3/8/2020", "3/15/2020", "3/22/2020", "3/29/2020", "4/5/2020", "4/12/2020", "4/19/2020", "4/26/2020", "5/3/2020", "5/10/2020", "5/17/2020", "5/24/2020", "5/31/2020", "6/7/2020", "6/14/2020", "6/21/2020", "6/28/2020", "7/5/2020", "7/12/2020", "7/19/2020", "7/26/2020", "8/2/2020", "8/9/2020", "8/16/2020", "8/23/2020", "8/30/2020"]

Related

split string into json object

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"

How can I filter everything but prices?

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

How to Validate GMT formatted date with Regular Expression

My date format input is like -Mon Nov 23 2015 10:24:04 GMT+0530 (India Standard Time)
I want validate above date with regularExpression like if the user given input formatt is Mon Nov 23+-}\ 2015 10:24:04 abcd GMT+0530 (India Standard Time) then validate that date with RegEx

JavaScript: new Date(Date.parse("A 49"))

new Date(Date.parse("A49"))
// Invalid Date
new Date(Date.parse("A 49"))
// Fri Jan 01 2049 00:00:00 GMT+0600 (Ekaterinburg Standard Time)
new Date(Date.parse("Aasdfashdkfjahslkdf 49"))
// Fri Jan 01 2049 00:00:00 GMT+0600 (Ekaterinburg Standard Time)
Why "A 49" is valid DateTime?
The parser ignored your A (because your A can be something like a separator or just some user text that has no relation to the date), and just picked the number and took it as the year. But if you add a letter to the number the parser cant recognize it as a number, and consequently can not convert it to an date.
Examples:
new Date(Date.parse("A 49"))
//Fri Jan 01 2049 00:00:00 GMT+0100 (W. Europe Standard Time)
new Date(Date.parse("OTHER 49"))
//Fri Jan 01 2049 00:00:00 GMT+0100 (W. Europe Standard Time)
new Date(Date.parse("OTHER 10"))
//Mon Oct 01 2001 00:00:00 GMT+0200 (W. Europe Daylight Time)
new Date(Date.parse("OTHER 10a"))
//Invalid Date
new Date(Date.parse("OTHER 10 10"))
//Wed Oct 10 2001 00:00:00 GMT+0200 (W. Europe Daylight Time)
More about Date.parse("...")
Because it's parsing A 49 and extracting only 49 and assuming it's start of 2049.

Difference between "new Date().valueOf()" and Date().valueOf()"

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.

Categories