需要仅使用日期更改日期时间 [英] Need to change date time with only date

查看:81
本文介绍了需要仅使用日期更改日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串类型变量,日期时间值为19/05/18 06:20:40

现在我想将此更改为仅像19/05/18这样的日期



我尝试了什么:



试过这个

格式(inDate,dd / MM / yy)

但它不适合我

I have a string type variable having date time value "19/05/18 06:20:40"
now i want to change this to only date like "19/05/18"

What I have tried:

tried this
Format(inDate, "dd/MM/yy")
but its not working for me

推荐答案

如果你愿意的话想要以特定格式显示日期,请阅读:

标准日期和时间格式字符串| Microsoft Docs [ ^ ]

自定义数字格式字符串| Microsoft Docs [ ^ ]



所以, DateTime.ToString() [ ^ ]应该完成这项工作:

If you would like to display date in specific format, please read this:
Standard Date and Time Format Strings | Microsoft Docs[^]
Custom Numeric Format Strings | Microsoft Docs[^]

So, DateTime.ToString()[^] should do the job:
Dim stringdate = "19/05/18 06:20:40"
Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
Dim mydate = DateTime.ParseExact(stringdate, "dd/MM/yy HH:mm:ss", provider)
Dim formatdate = mydate.Tostring("dd/MM/yy")
'prints 19-05-18 in Poland
'prints 19.05.18 in Germany
'prints 19/05/18 in USA





如您所见,日期格式取决于系统设置。如果日期分隔符不同于 / ,则必须使用 DateTime.ToString方法(字符串,IFormatProvider)(系统) [ ^ ]



As you can see a date format depends on system settings. In case when a date separator is different then "/", then you have to use DateTime.ToString Method (String, IFormatProvider) (System)[^]

Dim formatdate1 = mydate.Tostring("dd/MM/yy", New Globalization.CultureInfo("en-US"))


当你有一个String时,你应该使用String-Functions来处理它。在你的情况下,SubString可以解决问题...

When you have a String you should use String-Functions to handle it. In your case SubString could solve the Problem ...
OutString = inDate.SubString(0,8)



如果您的Source-Variable具有以下内容,则Format-Function将仅起作用(如您所发布的)日期类型...


The Format-Function will only work (like posted by you) if your Source-Variable has the Type of Date ...


Ralfs解决方案的替代方案可能是:

An alternative to Ralfs solution might be:
dim inDate as string = "19/05/18 06:20:40"
Console.WriteLine(inDate.Split(New Char() {" "c})(0))

这也可以正常工作日期部分的长度各不相同。

This will work correctly too if the length of the date part varies.


这篇关于需要仅使用日期更改日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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