【问题标题】:Get the last day of a month on powershell在powershell上获取一个月的最后一天
【发布时间】:2020-09-15 02:12:45
【问题描述】:

我正在尝试使用 PowerShell 在月份的第一天和最后一天放入 .txt 文件。

在下面的示例中,我试图获取 7 月的第一天和最后一天,但我只是得到第一天。脚本的第二行不起作用。

@PowerShell "(Get-Date).AddMonths(1).ToString('01/MM/yyyy')" >>dates.txt 
$LastDayInMonthString = "$($(get-date).AddMonths(1).ToString("dd/mm/yyy"))$LastDayInMonth" >>dates.txt

谁能告诉我怎么了?

我想要一个类似的 .txt 文件:01/07/2018, 31/07/2018
第一行写下个月的第一天,
第二行写那个月的最后一天。

【问题讨论】:

  • 用“..”包裹的powershell部分以第一行结束。你期望/想要什么输出。
  • 我想要一个类似的 .txt 文件:01/07/2018 31/07/2018。第一行写下个月的第一天,第二行写下该月的最后一天。但我不知道我做错了什么。
  • 加2个月,选择那个月的第一天(类似你已经做过的手表:01/MM/yyyy),减去一天。

标签: powershell


【解决方案1】:

更简单的解决方案是调用 DaysInMonth 函数

[DateTime]::DaysInMonth(2018, 11)

当前月份看起来像:

$today = get-date
$lastDay = [DateTime]::DaysInMonth($today.Year, $today.Month)
$firstDate = [DateTime]::new($today.Year, $today.Month, 1)
$lastDate  = [DateTime]::new($today.Year, $today.Month, $lastDay)

$firstDate
$lastDate

这也适用于任何阻碍夏令时变化和时区等可能发生的其他奇怪事情。

或者如果你只需要纯字符串:

(get-date -Format "yyyy/MM") + "/1"
(get-date -Format "yyyy/MM") + "/" + [DateTime]::DaysInMonth((get-date).Year, (get-date).Month)

【讨论】:

  • 但这会返回当前月份的数据而不是下一个月份的数据。
  • 该函数使用 get-date 作为输入。喂它你想要的任何年/月数。
  • 我不认为它或看起来很多当你得到下个月并将其放入所需的格式01/07/2018, 31/07/2018
  • 不错,干净的解决方案@jessehouwing!我喜欢它并在我的脚本中使用它来保存每个月最后一天的文件。
【解决方案2】:

一个简单的方法是取上一年的最后一天,然后加上 1..12 个月:

1..12 | % { (New-Object DateTime(2017,12,31)).AddMonths($_) }

输出将采用用户的日期/时间格式,在我的例子中是荷兰语:

woensdag 31 januari 2018 00:00:00
woensdag 28 februari 2018 00:00:00
zaterdag 31 maart 2018 00:00:00
maandag 30 april 2018 00:00:00
donderdag 31 mei 2018 00:00:00
zaterdag 30 juni 2018 00:00:00
dinsdag 31 juli 2018 00:00:00
vrijdag 31 augustus 2018 00:00:00
zondag 30 september 2018 00:00:00
woensdag 31 oktober 2018 00:00:00
vrijdag 30 november 2018 00:00:00
maandag 31 december 2018 00:00:00

如果需要,您可以根据需要对其进行格式化,例如

1..12 | % { (New-Object DateTime(2017,12,31)).AddMonths($_).ToString("yyyyMMdd") }

20180131
20180228
20180331
20180430
20180531
20180630
20180731
20180831
20180930
20181031
20181130
20181231

【讨论】:

    【解决方案3】:

    这看起来很简单

    $firstDate = [DateTime]::new($reportDate.Year, $reportDate.Month, 1)
    $lastDate=$firstDate.AddMonths(1).AddDays(-1)
    

    【讨论】:

      【解决方案4】:

      Edit 删除了仅针对日期不必要的时间调整

      在 PowerShell 中获取 下一个月的第一天和最后一天

      $CIGB = New-Object System.Globalization.CultureInfo('en-GB')
      '{0}, {1}' -f (Get-Date -Day 1).AddMonths(1).ToString('d',$CIGB),
        (Get-Date -Day 1).AddMonths(2).AddDays(-1).ToString('d',$CIGB)|sc dates.txt
      

      $CIGB 对我来说是必需的,因为我的本地日期分隔符覆盖了 /
      如果您的短日期格式'd' 返回dd/MM/yyyy 第一行,则可以删除,$CIGB

      01/07/2018, 31/07/2018
      

      这可以被包裹在一个单行(虽然很长)中。

      powershell -nop -c "$CIGB=New-Object System.Globalization.CultureInfo('en-GB');'{0}, {1}' -f (Get-Date -Day 1).AddMonths(1).ToString('d',$CIGB),(Get-Date -Day 1).AddMonths(2).AddDays(-1).ToString('d',$CIGB)">>dates.txt
      

      > type dates.txt
      01/07/2018, 31/07/2018
      

      【讨论】:

      • 好!如果我想在日期之后放一个','?喜欢:2018 年 1 月 7 日,2018 年 7 月 31 日,
      • 将字符串格式运算符更改为'{0}, {1}' -f(插入日期代替{0}{1}
      • 工作正常!谢谢大家。
      【解决方案5】:

      可以将字符串传递给 Get-Date。 -Format 参数也接受字符串/变量。

      # Get the previous month
      $LastMonth = (Get-Date).AddMonths(-1)
      
      # Format the first day of the month
      $FirstDayOfMonth = $LastMonth | Get-Date -Format "yyyy/MM/01"
      
      # Calculate the number of days (last day) in the month
      $LastDay = [DateTime]::DaysInMonth($LastMonth.Year, $LastMonth.Month)
      
      # Use it in the format parameter
      $LastDayOfMonth = $LastMonth | Get-Date -Format "yyyy/MM/$lastDay"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        • 2013-11-25
        • 2021-07-30
        相关资源
        最近更新 更多