获取日期格式/文化 [英] Get-Date formatting/culture

查看:49
本文介绍了获取日期格式/文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何指定输入字符串的哪个部分是日期和月份?

How do I specify what part of my input string is the date and month?

如果输入的是 01/10/2017 ,则可以读为 2017年10月1日 2017年1月10日。两者都是正确的。

If the input is 01/10/2017, this can be read as 1st Oct 2017 and 10th Jan 2017. Both are correct.

我想明确指出 01 是日期而 10 是月份,因此无论语言环境和时间格式如何,我都可以获得一致的结果。

I want to be explicit that 01 is date and 10 is month, so that irrespective of locale and time format I can get a consistent result.

示例代码:

get-date  -Date '01/10/2017'

输出为:

Tuesday, January 10, 2017 12:00:00 AM

所需的输出为:

Sunday, October 01, 2017 12:00:00 AM


推荐答案

我为您提供解决方案。它要求将文化作为参数之一。

I have a solution for you. It requires that the culture as one of the arguments.

([datetime]::ParseExact($date,"dd/MM/yyyy",[Globalization.CultureInfo]::CreateSpecificCulture('en-GB')))

不必指定文化。但是,它的参数会这样做,否则会出现错误:

A culture does not have to be specified. However, the argument for it does, otherwise you will get an error:


找不到 ParseExact的重载和参数计数: 2。

Cannot find an overload for "ParseExact" and the argument count: "2".

[cultureinfo] :: InvariantCulture $ null 可以用作第三个参数:

[cultureinfo]::InvariantCulture or $null can be used as the third argument:

$date = "01/10/2017"
[datetime]::ParseExact($date, "dd/MM/yyyy", [cultureinfo]::InvariantCulture)
[datetime]::ParseExact($date, "dd/MM/yyyy", $null)

在所有三种情况下的输出

01 October 2017 00:00:00

这篇关于获取日期格式/文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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