PowerShell - An weird issue related to convertfrom-string

281 views Asked by At

So I am trying to get average lengths of the four seasons in the 21st century using PowerShell as a self-imposed programming challenge.

My idea is to get values from a text file, create a [PSCustomObject] and assign values to its noteproperties each line, add year to the date and convert the dates to [datetime] and add to another array, then loop through the second array using index and get season length using new-timespan and add to a third array, and measure-object third array, forgive me if this may sound confusing in English but it really is very simple in code.

Now I get the dates of equinoxes and solstices from here:Solstices and Equinoxes: 2001 to 2100

Using Notepad++ to format the dates I got this:

2001   Mar 20  13:31   Jun 21  07:38   Sep 22  23:05   Dec 21  19:22
2002   Mar 20  19:16   Jun 21  13:25   Sep 23  04:56   Dec 22  01:15
2003   Mar 21  01:00   Jun 21  19:11   Sep 23  10:47   Dec 22  07:04
2004   Mar 20  06:49   Jun 21  00:57   Sep 22  16:30   Dec 21  12:42
2005   Mar 20  12:34   Jun 21  06:46   Sep 22  22:23   Dec 21  18:35
2006   Mar 20  18:25   Jun 21  12:26   Sep 23  04:04   Dec 22  00:22
2007   Mar 21  00:07   Jun 21  18:06   Sep 23  09:51   Dec 22  06:08
2008   Mar 20  05:49   Jun 21  00:00   Sep 22  15:45   Dec 21  12:04
2009   Mar 20  11:44   Jun 21  05:45   Sep 22  21:18   Dec 21  17:47
2010   Mar 20  17:32   Jun 21  11:28   Sep 23  03:09   Dec 21  23:38
2011   Mar 20  23:21   Jun 21  17:16   Sep 23  09:05   Dec 22  05:30
2012   Mar 20  05:15   Jun 20  23:08   Sep 22  14:49   Dec 21  11:12
2013   Mar 20  11:02   Jun 21  05:04   Sep 22  20:44   Dec 21  17:11
2014   Mar 20  16:57   Jun 21  10:52   Sep 23  02:30   Dec 21  23:03
2015   Mar 20  22:45   Jun 21  16:38   Sep 23  08:20   Dec 22  04:48
2016   Mar 20  04:31   Jun 20  22:35   Sep 22  14:21   Dec 21  10:45
2017   Mar 20  10:29   Jun 21  04:25   Sep 22  20:02   Dec 21  16:29
2018   Mar 20  16:15   Jun 21  10:07   Sep 23  01:54   Dec 21  22:22
2019   Mar 20  21:58   Jun 21  15:54   Sep 23  07:50   Dec 22  04:19
2020   Mar 20  03:50   Jun 20  21:43   Sep 22  13:31   Dec 21  10:03
2021   Mar 20  09:37   Jun 21  03:32   Sep 22  19:21   Dec 21  15:59
2022   Mar 20  15:33   Jun 21  09:14   Sep 23  01:04   Dec 21  21:48
2023   Mar 20  21:25   Jun 21  14:58   Sep 23  06:50   Dec 22  03:28
2024   Mar 20  03:07   Jun 20  20:51   Sep 22  12:44   Dec 21  09:20
2025   Mar 20  09:02   Jun 21  02:42   Sep 22  18:20   Dec 21  15:03
2026   Mar 20  14:46   Jun 21  08:25   Sep 23  00:06   Dec 21  20:50
2027   Mar 20  20:25   Jun 21  14:11   Sep 23  06:02   Dec 22  02:43
2028   Mar 20  02:17   Jun 20  20:02   Sep 22  11:45   Dec 21  08:20
2029   Mar 20  08:01   Jun 21  01:48   Sep 22  17:37   Dec 21  14:14
2030   Mar 20  13:51   Jun 21  07:31   Sep 22  23:27   Dec 21  20:09
2031   Mar 20  19:41   Jun 21  13:17   Sep 23  05:15   Dec 22  01:56
2032   Mar 20  01:23   Jun 20  19:09   Sep 22  11:11   Dec 21  07:57
2033   Mar 20  07:23   Jun 21  01:01   Sep 22  16:52   Dec 21  13:45
2034   Mar 20  13:18   Jun 21  06:45   Sep 22  22:41   Dec 21  19:35
2035   Mar 20  19:03   Jun 21  12:33   Sep 23  04:39   Dec 22  01:31
2036   Mar 20  01:02   Jun 20  18:31   Sep 22  10:23   Dec 21  07:12
2037   Mar 20  06:50   Jun 21  00:22   Sep 22  16:13   Dec 21  13:08
2038   Mar 20  12:40   Jun 21  06:09   Sep 22  22:02   Dec 21  19:01
2039   Mar 20  18:32   Jun 21  11:58   Sep 23  03:50   Dec 22  00:41
2040   Mar 20  00:11   Jun 20  17:46   Sep 22  09:44   Dec 21  06:33
2041   Mar 20  06:07   Jun 20  23:37   Sep 22  15:27   Dec 21  12:19
2042   Mar 20  11:53   Jun 21  05:16   Sep 22  21:11   Dec 21  18:04
2043   Mar 20  17:29   Jun 21  10:59   Sep 23  03:07   Dec 22  00:02
2044   Mar 19  23:20   Jun 20  16:50   Sep 22  08:47   Dec 21  05:43
2045   Mar 20  05:08   Jun 20  22:34   Sep 22  14:33   Dec 21  11:36
2046   Mar 20  10:58   Jun 21  04:15   Sep 22  20:22   Dec 21  17:28
2047   Mar 20  16:52   Jun 21  10:02   Sep 23  02:07   Dec 21  23:07
2048   Mar 19  22:34   Jun 20  15:54   Sep 22  08:01   Dec 21  05:02
2049   Mar 20  04:28   Jun 20  21:47   Sep 22  13:42   Dec 21  10:51
2050   Mar 20  10:20   Jun 21  03:33   Sep 22  19:29   Dec 21  16:39
2051   Mar 20  15:58   Jun 21  09:17   Sep 23  01:26   Dec 21  22:33
2052   Mar 19  21:56   Jun 20  15:16   Sep 22  07:16   Dec 21  04:18
2053   Mar 20  03:46   Jun 20  21:03   Sep 22  13:05   Dec 21  10:09
2054   Mar 20  09:35   Jun 21  02:47   Sep 22  19:00   Dec 21  16:10
2055   Mar 20  15:28   Jun 21  08:39   Sep 23  00:48   Dec 21  21:56
2056   Mar 19  21:11   Jun 20  14:29   Sep 22  06:40   Dec 21  03:52
2057   Mar 20  03:08   Jun 20  20:19   Sep 22  12:23   Dec 21  09:42
2058   Mar 20  09:04   Jun 21  02:03   Sep 22  18:07   Dec 21  15:24
2059   Mar 20  14:44   Jun 21  07:47   Sep 23  00:03   Dec 21  21:18
2060   Mar 19  20:37   Jun 20  13:44   Sep 22  05:47   Dec 21  03:00
2061   Mar 20  02:26   Jun 20  19:33   Sep 22  11:31   Dec 21  08:49
2062   Mar 20  08:07   Jun 21  01:10   Sep 22  17:19   Dec 21  14:42
2063   Mar 20  13:59   Jun 21  07:02   Sep 22  23:08   Dec 21  20:22
2064   Mar 19  19:40   Jun 20  12:47   Sep 22  04:58   Dec 21  02:10
2065   Mar 20  01:27   Jun 20  18:31   Sep 22  10:41   Dec 21  07:59
2066   Mar 20  07:19   Jun 21  00:16   Sep 22  16:27   Dec 21  13:45
2067   Mar 20  12:55   Jun 21  05:56   Sep 22  22:20   Dec 21  19:44
2068   Mar 19  18:51   Jun 20  11:55   Sep 22  04:09   Dec 21  01:34
2069   Mar 20  00:44   Jun 20  17:40   Sep 22  09:51   Dec 21  07:21
2070   Mar 20  06:35   Jun 20  23:22   Sep 22  15:45   Dec 21  13:19
2071   Mar 20  12:36   Jun 21  05:21   Sep 22  21:39   Dec 21  19:05
2072   Mar 19  18:19   Jun 20  11:12   Sep 22  03:26   Dec 21  00:54
2073   Mar 20  00:12   Jun 20  17:06   Sep 22  09:14   Dec 21  06:50
2074   Mar 20  06:09   Jun 20  22:59   Sep 22  15:04   Dec 21  12:36
2075   Mar 20  11:48   Jun 21  04:41   Sep 22  21:00   Dec 21  18:28
2076   Mar 19  17:37   Jun 20  10:35   Sep 22  02:48   Dec 21  00:12
2077   Mar 19  23:30   Jun 20  16:23   Sep 22  08:35   Dec 21  06:00
2078   Mar 20  05:11   Jun 20  21:58   Sep 22  14:25   Dec 21  11:59
2079   Mar 20  11:03   Jun 21  03:51   Sep 22  20:15   Dec 21  17:46
2080   Mar 19  16:43   Jun 20  09:33   Sep 22  01:55   Dec 20  23:31
2081   Mar 19  22:34   Jun 20  15:16   Sep 22  07:38   Dec 21  05:22
2082   Mar 20  04:32   Jun 20  21:04   Sep 22  13:24   Dec 21  11:06
2083   Mar 20  10:08   Jun 21  02:41   Sep 22  19:10   Dec 21  16:51
2084   Mar 19  15:58   Jun 20  08:39   Sep 22  00:58   Dec 20  22:40
2085   Mar 19  21:53   Jun 20  14:33   Sep 22  06:43   Dec 21  04:29
2086   Mar 20  03:36   Jun 20  20:11   Sep 22  12:33   Dec 21  10:24
2087   Mar 20  09:27   Jun 21  02:05   Sep 22  18:27   Dec 21  16:07
2088   Mar 19  15:16   Jun 20  07:57   Sep 22  00:18   Dec 20  21:56
2089   Mar 19  21:07   Jun 20  13:43   Sep 22  06:07   Dec 21  03:53
2090   Mar 20  03:03   Jun 20  19:37   Sep 22  12:01   Dec 21  09:45
2091   Mar 20  08:40   Jun 21  01:17   Sep 22  17:49   Dec 21  15:37
2092   Mar 19  14:33   Jun 20  07:14   Sep 21  23:41   Dec 20  21:31
2093   Mar 19  20:35   Jun 20  13:08   Sep 22  05:30   Dec 21  03:21
2094   Mar 20  02:20   Jun 20  18:40   Sep 22  11:15   Dec 21  09:11
2095   Mar 20  08:14   Jun 21  00:38   Sep 22  17:10   Dec 21  15:00
2096   Mar 19  14:03   Jun 20  06:31   Sep 21  22:55   Dec 20  20:46
2097   Mar 19  19:49   Jun 20  12:14   Sep 22  04:37   Dec 21  02:38
2098   Mar 20  01:38   Jun 20  18:01   Sep 22  10:22   Dec 21  08:19
2099   Mar 20  07:17   Jun 20  23:41   Sep 22  16:10   Dec 21  14:04
2100   Mar 20  13:04   Jun 21  05:32   Sep 22  22:00   Dec 21  19:51

Then I used these codes on PowerShell 7.1 x64 on Windows 10:

$timetable = Get-Content .\Desktop\Equinox-Solstice.txt | Convertfrom-String -Templatefile .\Desktop\template.txt
$count=$timetable.count
$timetable1=@()
for ($i=0;$i -lt $count;$i++) {
$year=[string]$timetable[$i].year
$mequi=[datetime]($year+" "+$timetable[$i].marequi)
$jsols=[datetime]($year+" "+$timetable[$i].junsols)
$sequi=[datetime]($year+" "+$timetable[$i].sepequi)
$dsols=[datetime]($year+" "+$timetable[$i].decsols)
$timetable1+=[pscustomobject]@{year=$year;mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols}
}

With this as template:

{[int]year*:2001}   {[string]marequi:Mar 20  13:31}   {[string]junsols:Jun 21  07:38}   {[string]sepequi:Sep 22  23:05}   {[string]decsols:Dec 21  19:22}
{[int]year*:2002}   {[string]marequi:Mar 20  19:16}   {[string]junsols:Jun 21  13:25}   {[string]sepequi:Sep 23  04:56}   {[string]decsols:Dec 22  01:15}
{[int]year*:2003}   {[string]marequi:Mar 21  01:00}   {[string]junsols:Jun 21  19:11}   {[string]sepequi:Sep 23  10:47}   {[string]decsols:Dec 22  07:04}
{[int]year*:2004}   {[string]marequi:Mar 20  06:49}   {[string]junsols:Jun 21  00:57}   {[string]sepequi:Sep 22  16:30}   {[string]decsols:Dec 21  12:42}
{[int]year*:2005}   {[string]marequi:Mar 20  12:34}   {[string]junsols:Jun 21  06:46}   {[string]sepequi:Sep 22  22:23}   {[string]decsols:Dec 21  18:35}
{[int]year*:2006}   {[string]marequi:Mar 20  18:25}   {[string]junsols:Jun 21  12:26}   {[string]sepequi:Sep 23  04:04}   {[string]decsols:Dec 22  00:22}
{[int]year*:2007}   {[string]marequi:Mar 21  00:07}   {[string]junsols:Jun 21  18:06}   {[string]sepequi:Sep 23  09:51}   {[string]decsols:Dec 22  06:08}
{[int]year*:2008}   {[string]marequi:Mar 20  05:49}   {[string]junsols:Jun 21  00:00}   {[string]sepequi:Sep 22  15:45}   {[string]decsols:Dec 21  12:04}
{[int]year*:2009}   {[string]marequi:Mar 20  11:44}   {[string]junsols:Jun 21  05:45}   {[string]sepequi:Sep 22  21:18}   {[string]decsols:Dec 21  17:47}
{[int]year*:2010}   {[string]marequi:Mar 20  17:32}   {[string]junsols:Jun 21  11:28}   {[string]sepequi:Sep 23  03:09}   {[string]decsols:Dec 21  23:38}

And when I ran the command, I got lots of error messages, all of them are like this:

InvalidArgument:
Line |
   3 |  $mequi=[datetime]($year+" "+$timetable[$i].marequi)
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot convert value "2093 " to type "System.DateTime". Error: "String '2093 ' was not recognized as a valid DateTime."

I have checked thoroughly and found all of them are from $mequi, which stands for March Equinox, I realised the dates must be missing, then I typed $timetable and confirmed lots of March Equinox dates are missing, and only March Equinox dates are missing, I then used this command:

for($i=0;$i -lt 99;$i++){if ($timetable[$i].marequi -eq $null){$i}}

And found out 46 of them are missing, but the dates in other 3 categories are not missing, just March Equinox, the indexes of the missing dates are the following: 11, 15, 19, 23, 27, 31, 35, 39, 40, 43, 44, 47, 48, 51, 52, 55, 56, 59, 60, 63, 64, 67, 68, 69, 71, 72, 73, 75, 76, 77, 79, 80, 81, 83, 84, 85, 87, 88, 89, 91, 92, 93, 95, 96, 97, 98

Any ideas? Can someone help me, please?

Update3: So I used the complete code:

$timetable = Get-Content .\Desktop\Equinox-Solstice.txt | Convertfrom-String -Templatefile .\Desktop\template.txt
$count=$timetable.count
[array]$timetable1 = 0..99 | foreach-object {
$year=[string]$timetable[$_].year
$mequi=[datetime]($year+" "+$timetable[$_].marequi)
$jsols=[datetime]($year+" "+$timetable[$_].junsols)
$sequi=[datetime]($year+" "+$timetable[$_].sepequi)
$dsols=[datetime]($year+" "+$timetable[$_].decsols)
[pscustomobject]@{year=$year;mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols}
}
[array]$seasons=0..98 | foreach-object {
$year=$timetable1[$_].year
$spring=[double](New-Timespan -Start $timetable1[$_].mequi -End $timetable1[$_].jsols).totaldays
$summer=[double](New-Timespan -Start $timetable1[$_].jsols -End $timetable1[$_].sequi).totaldays
$autumn=[double](New-Timespan -Start $timetable1[$_].sequi -End $timetable1[$_].dsols).totaldays
$winter=[double](New-Timespan -Start $timetable1[$_].dsols -End $timetable1[$_+1].mequi).totaldays
[pscustomobject]@{year=$year;spring=$spring;summer=$summer;autumn=$autumn;winter=$winter}
}
$meanspring=($seasons.spring | Measure-Object -Average).average
$meansummer=($seasons.summer | Measure-Object -Average).average
$meanautumn=($seasons.autumn | Measure-Object -Average).average
$meanwinter=($seasons.winter | Measure-Object -Average).average
$meanyear=$meanspring+$meansummer+$meanautumn+$meanwinter
Write-Host "Mean Spring Length: $meanspring days, Mean Summer Length: $meansummer days, Mean Autumn Length: $meanautumn days, Mean Winter Length: $meanwinter days, Mean Solar Year Length: $meanyear days"

And this is what I got:

Mean Spring Length: 92.7203563411897 days, Mean Summer Length: 93.6684764309764 days, Mean Autumn Length: 89.8785914702581 days, Mean Winter Length: 88.9748106060606 days, Mean Solar Year Length: 365.242234848485 days.

Update: I am now editing the question because I want to share a better method.

With a little more effort at find and replace in Notepad++ I was able to obtain this:

"year","marequi","junsols","sepequi","decsols"
"2001","Mar 20  13:31","Jun 21  07:38","Sep 22  23:05","Dec 21  19:22"
"2002","Mar 20  19:16","Jun 21  13:25","Sep 23  04:56","Dec 22  01:15"
"2003","Mar 21  01:00","Jun 21  19:11","Sep 23  10:47","Dec 22  07:04"
"2004","Mar 20  06:49","Jun 21  00:57","Sep 22  16:30","Dec 21  12:42"
"2005","Mar 20  12:34","Jun 21  06:46","Sep 22  22:23","Dec 21  18:35"
"2006","Mar 20  18:25","Jun 21  12:26","Sep 23  04:04","Dec 22  00:22"
"2007","Mar 21  00:07","Jun 21  18:06","Sep 23  09:51","Dec 22  06:08"
"2008","Mar 20  05:49","Jun 21  00:00","Sep 22  15:45","Dec 21  12:04"
"2009","Mar 20  11:44","Jun 21  05:45","Sep 22  21:18","Dec 21  17:47"
"2010","Mar 20  17:32","Jun 21  11:28","Sep 23  03:09","Dec 21  23:38"
"2011","Mar 20  23:21","Jun 21  17:16","Sep 23  09:05","Dec 22  05:30"
"2012","Mar 20  05:15","Jun 20  23:08","Sep 22  14:49","Dec 21  11:12"
"2013","Mar 20  11:02","Jun 21  05:04","Sep 22  20:44","Dec 21  17:11"
"2014","Mar 20  16:57","Jun 21  10:52","Sep 23  02:30","Dec 21  23:03"
"2015","Mar 20  22:45","Jun 21  16:38","Sep 23  08:20","Dec 22  04:48"
"2016","Mar 20  04:31","Jun 20  22:35","Sep 22  14:21","Dec 21  10:45"
"2017","Mar 20  10:29","Jun 21  04:25","Sep 22  20:02","Dec 21  16:29"
"2018","Mar 20  16:15","Jun 21  10:07","Sep 23  01:54","Dec 21  22:22"
"2019","Mar 20  21:58","Jun 21  15:54","Sep 23  07:50","Dec 22  04:19"
"2020","Mar 20  03:50","Jun 20  21:43","Sep 22  13:31","Dec 21  10:03"
"2021","Mar 20  09:37","Jun 21  03:32","Sep 22  19:21","Dec 21  15:59"
"2022","Mar 20  15:33","Jun 21  09:14","Sep 23  01:04","Dec 21  21:48"
"2023","Mar 20  21:25","Jun 21  14:58","Sep 23  06:50","Dec 22  03:28"
"2024","Mar 20  03:07","Jun 20  20:51","Sep 22  12:44","Dec 21  09:20"
"2025","Mar 20  09:02","Jun 21  02:42","Sep 22  18:20","Dec 21  15:03"
"2026","Mar 20  14:46","Jun 21  08:25","Sep 23  00:06","Dec 21  20:50"
"2027","Mar 20  20:25","Jun 21  14:11","Sep 23  06:02","Dec 22  02:43"
"2028","Mar 20  02:17","Jun 20  20:02","Sep 22  11:45","Dec 21  08:20"
"2029","Mar 20  08:01","Jun 21  01:48","Sep 22  17:37","Dec 21  14:14"
"2030","Mar 20  13:51","Jun 21  07:31","Sep 22  23:27","Dec 21  20:09"
"2031","Mar 20  19:41","Jun 21  13:17","Sep 23  05:15","Dec 22  01:56"
"2032","Mar 20  01:23","Jun 20  19:09","Sep 22  11:11","Dec 21  07:57"
"2033","Mar 20  07:23","Jun 21  01:01","Sep 22  16:52","Dec 21  13:45"
"2034","Mar 20  13:18","Jun 21  06:45","Sep 22  22:41","Dec 21  19:35"
"2035","Mar 20  19:03","Jun 21  12:33","Sep 23  04:39","Dec 22  01:31"
"2036","Mar 20  01:02","Jun 20  18:31","Sep 22  10:23","Dec 21  07:12"
"2037","Mar 20  06:50","Jun 21  00:22","Sep 22  16:13","Dec 21  13:08"
"2038","Mar 20  12:40","Jun 21  06:09","Sep 22  22:02","Dec 21  19:01"
"2039","Mar 20  18:32","Jun 21  11:58","Sep 23  03:50","Dec 22  00:41"
"2040","Mar 20  00:11","Jun 20  17:46","Sep 22  09:44","Dec 21  06:33"
"2041","Mar 20  06:07","Jun 20  23:37","Sep 22  15:27","Dec 21  12:19"
"2042","Mar 20  11:53","Jun 21  05:16","Sep 22  21:11","Dec 21  18:04"
"2043","Mar 20  17:29","Jun 21  10:59","Sep 23  03:07","Dec 22  00:02"
"2044","Mar 19  23:20","Jun 20  16:50","Sep 22  08:47","Dec 21  05:43"
"2045","Mar 20  05:08","Jun 20  22:34","Sep 22  14:33","Dec 21  11:36"
"2046","Mar 20  10:58","Jun 21  04:15","Sep 22  20:22","Dec 21  17:28"
"2047","Mar 20  16:52","Jun 21  10:02","Sep 23  02:07","Dec 21  23:07"
"2048","Mar 19  22:34","Jun 20  15:54","Sep 22  08:01","Dec 21  05:02"
"2049","Mar 20  04:28","Jun 20  21:47","Sep 22  13:42","Dec 21  10:51"
"2050","Mar 20  10:20","Jun 21  03:33","Sep 22  19:29","Dec 21  16:39"
"2051","Mar 20  15:58","Jun 21  09:17","Sep 23  01:26","Dec 21  22:33"
"2052","Mar 19  21:56","Jun 20  15:16","Sep 22  07:16","Dec 21  04:18"
"2053","Mar 20  03:46","Jun 20  21:03","Sep 22  13:05","Dec 21  10:09"
"2054","Mar 20  09:35","Jun 21  02:47","Sep 22  19:00","Dec 21  16:10"
"2055","Mar 20  15:28","Jun 21  08:39","Sep 23  00:48","Dec 21  21:56"
"2056","Mar 19  21:11","Jun 20  14:29","Sep 22  06:40","Dec 21  03:52"
"2057","Mar 20  03:08","Jun 20  20:19","Sep 22  12:23","Dec 21  09:42"
"2058","Mar 20  09:04","Jun 21  02:03","Sep 22  18:07","Dec 21  15:24"
"2059","Mar 20  14:44","Jun 21  07:47","Sep 23  00:03","Dec 21  21:18"
"2060","Mar 19  20:37","Jun 20  13:44","Sep 22  05:47","Dec 21  03:00"
"2061","Mar 20  02:26","Jun 20  19:33","Sep 22  11:31","Dec 21  08:49"
"2062","Mar 20  08:07","Jun 21  01:10","Sep 22  17:19","Dec 21  14:42"
"2063","Mar 20  13:59","Jun 21  07:02","Sep 22  23:08","Dec 21  20:22"
"2064","Mar 19  19:40","Jun 20  12:47","Sep 22  04:58","Dec 21  02:10"
"2065","Mar 20  01:27","Jun 20  18:31","Sep 22  10:41","Dec 21  07:59"
"2066","Mar 20  07:19","Jun 21  00:16","Sep 22  16:27","Dec 21  13:45"
"2067","Mar 20  12:55","Jun 21  05:56","Sep 22  22:20","Dec 21  19:44"
"2068","Mar 19  18:51","Jun 20  11:55","Sep 22  04:09","Dec 21  01:34"
"2069","Mar 20  00:44","Jun 20  17:40","Sep 22  09:51","Dec 21  07:21"
"2070","Mar 20  06:35","Jun 20  23:22","Sep 22  15:45","Dec 21  13:19"
"2071","Mar 20  12:36","Jun 21  05:21","Sep 22  21:39","Dec 21  19:05"
"2072","Mar 19  18:19","Jun 20  11:12","Sep 22  03:26","Dec 21  00:54"
"2073","Mar 20  00:12","Jun 20  17:06","Sep 22  09:14","Dec 21  06:50"
"2074","Mar 20  06:09","Jun 20  22:59","Sep 22  15:04","Dec 21  12:36"
"2075","Mar 20  11:48","Jun 21  04:41","Sep 22  21:00","Dec 21  18:28"
"2076","Mar 19  17:37","Jun 20  10:35","Sep 22  02:48","Dec 21  00:12"
"2077","Mar 19  23:30","Jun 20  16:23","Sep 22  08:35","Dec 21  06:00"
"2078","Mar 20  05:11","Jun 20  21:58","Sep 22  14:25","Dec 21  11:59"
"2079","Mar 20  11:03","Jun 21  03:51","Sep 22  20:15","Dec 21  17:46"
"2080","Mar 19  16:43","Jun 20  09:33","Sep 22  01:55","Dec 20  23:31"
"2081","Mar 19  22:34","Jun 20  15:16","Sep 22  07:38","Dec 21  05:22"
"2082","Mar 20  04:32","Jun 20  21:04","Sep 22  13:24","Dec 21  11:06"
"2083","Mar 20  10:08","Jun 21  02:41","Sep 22  19:10","Dec 21  16:51"
"2084","Mar 19  15:58","Jun 20  08:39","Sep 22  00:58","Dec 20  22:40"
"2085","Mar 19  21:53","Jun 20  14:33","Sep 22  06:43","Dec 21  04:29"
"2086","Mar 20  03:36","Jun 20  20:11","Sep 22  12:33","Dec 21  10:24"
"2087","Mar 20  09:27","Jun 21  02:05","Sep 22  18:27","Dec 21  16:07"
"2088","Mar 19  15:16","Jun 20  07:57","Sep 22  00:18","Dec 20  21:56"
"2089","Mar 19  21:07","Jun 20  13:43","Sep 22  06:07","Dec 21  03:53"
"2090","Mar 20  03:03","Jun 20  19:37","Sep 22  12:01","Dec 21  09:45"
"2091","Mar 20  08:40","Jun 21  01:17","Sep 22  17:49","Dec 21  15:37"
"2092","Mar 19  14:33","Jun 20  07:14","Sep 21  23:41","Dec 20  21:31"
"2093","Mar 19  20:35","Jun 20  13:08","Sep 22  05:30","Dec 21  03:21"
"2094","Mar 20  02:20","Jun 20  18:40","Sep 22  11:15","Dec 21  09:11"
"2095","Mar 20  08:14","Jun 21  00:38","Sep 22  17:10","Dec 21  15:00"
"2096","Mar 19  14:03","Jun 20  06:31","Sep 21  22:55","Dec 20  20:46"
"2097","Mar 19  19:49","Jun 20  12:14","Sep 22  04:37","Dec 21  02:38"
"2098","Mar 20  01:38","Jun 20  18:01","Sep 22  10:22","Dec 21  08:19"
"2099","Mar 20  07:17","Jun 20  23:41","Sep 22  16:10","Dec 21  14:04"
"2100","Mar 20  13:04","Jun 21  05:32","Sep 22  22:00","Dec 21  19:51"

Save as seasons.csv

Now use these codes:

$Seasons=Import-Csv path\to\seasons.csv | % {
$MEqui=[DateTime]$([string]($_.Year )+$_.marequi)
$JSols=[DateTime]$([string]($_.Year )+$_.junsols)
$SEqui=[DateTime]$([string]($_.Year )+$_.sepequi)
$DSols=[DateTime]$([string]($_.Year )+$_.decsols)
[pscustomobject]@{mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols}
}
$Seasons | %{
$MEqui=$MEqui.ToString(yyyy-MM-ddTHH:mm:ssZ)
$JSols=$JSols.ToString(yyyy-MM-ddTHH:mm:ssZ)
$SEqui=$SEqui.ToString(yyyy-MM-ddTHH:mm:ssZ)
$DSols=$DSols.ToString(yyyy-MM-ddTHH:mm:ssZ)
[pscustomobject]@{MarchEquinox=$mequi;JuneSolstice=$jsols;SeptemberEquinox=$sequi;DecemberSolstice=$dsols} | Export-Csv path\to\output.csv -NoTypeInformation -Append
}

And the rest of the steps are the same (just replace $timetable1 with $seasons), this approach is better because importing from and exporting to csv is much easier and far more reliable.

1

There are 1 answers

1
iRon On BEST ANSWER

For the template issue, it appears that you have only one sample value (Jun 21) for the junsols column, if you update that, you will see that it works just fine.
In general, I would recommend the following template to cover all the dates:

{[int]year*:0000}   {[string]marequi:Mar 00  00:00}   {[string]junsols:Jun 00  00:00}   {[string]sepequi:Sep 00  00:00}   {[string]decsols:Dec 00  00:00}
{[int]year*:9999}   {[string]marequi:Mar 99  99:99}   {[string]junsols:Jun 99  99:99}   {[string]sepequi:Sep 99  99:99}   {[string]decsols:Dec 99  99:99}

Also (as a side note): try to avoid using the increase assignment operator (+=) to create a collection as it is exponential expensive