在powershell上获取一个月的最后一天 [英] Get the last day of a month on powershell

查看:97
本文介绍了在powershell上获取一个月的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to put in a .txt file the first day and the last day of the months using PowerShell.

在下面的示例中,我试图获得 7 月的第一天和最后一天,但我刚刚获得了第一天.脚本的第二行不起作用.

In the exemple below i was trying to get the first and the last day of July, but i'm just getting the first day. The second line of the script isn't working.

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

有人可以告诉我有什么问题吗?

Someone can say me what is wrong?

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

I wanted a .txt file like it: 01/07/2018, 31/07/2018.
The first line write the first day of next month,
and second line write the last day of that month.

推荐答案

Edit 删除了 for only date 不必要的时间调整

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

In PowerShell to get the first day and last of next month

$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 可以删除.

The $CIGB is neccessary for me because my local date separator overrides the /
If your short date format 'd' returns dd/MM/yyyy the first line and the ,$CIGB can be removed.

01/07/2018, 31/07/2018

这可以包含在一行(虽然很长)中.

This can be wrapped in a single (albeit quite long) line.

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

这篇关于在powershell上获取一个月的最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆