使用Now()解析日期格式 [英] Date format parsing using Now()

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

问题描述

您好,

我需要比较winforms中的日期,使用vb.net

我正在尝试使用

Hello,
I need to compare dates in winforms, using vb.net
I am trying using

If Now() >= DateTime.Parse("17.01.2013 08:47:10 PM")





问题是我的日期是在dd / mm / yyyy格式但是当此应用程序在格式为mm / dd / yy的系统上运行时,会出现问题。



如何提供系统日期(使用NOW())以dd / mm / yyyy格式获得,以便可以与我给定的日期进行比较?



谢谢



The problem is my date is in dd/mm/yyyy format but when this application runs on a system where the format is mm/dd/yy, it gives problem.

How can I bring the system date (obtained using NOW()) in dd/mm/yyyy format so it could be compared with my given date?

Thanks

推荐答案

你不应该依赖服务器的本地设置,而应该决定你想要的格式。



其中一种方法是使用: MSDN:使用InvariantCulture属性 [ ^ ] - InvariantCulture属性既不代表中性也不代表特定文化。它代表了对文化不敏感的第三种文化。它与英语有关,但与国家或地区无关。



Ex:

You should not rely on the local settings of the server instead you should be deciding what format you want.

One of the ways is to use: MSDN: Using the InvariantCulture Property[^] - The InvariantCulture property represents neither a neutral nor a specific culture. It represents a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.

Ex:
String someDateS = currDate.ToString(CultureInfo.InvariantCulture);
String otherDateS = DateTime.Parse(otherDate, CultureInfo.InvariantCulture); 
// Compare now
if(someDateS.Equals(otherDateS))
{
}


虽然您可以将日期比较为字符串,但它是不明智的。 现在返回一个与应有的可比性的日期时间结构。您在此处粘贴的代码行中采用的方法仍然很好,但您必须查看相反的方向:make DateTime.Parse处理您的日期格式。你可以这样做,因为Parse 方法有一个重载 [ ^ ]采用IFormatProvider。如果此格式特定于文化,则传递文化:

Although you can compare dates as string, it is not wise. "Now" return a datetime structure, that is comparable as it should be. The approach you have taken in your code row pasted here is still good, but you have to look in opposite direction: make DateTime.Parse process your date format. You can do that, since the Parse method has an overload[^] that takes an IFormatProvider. If this format is specific to a culture, pass a culture:
DateTime.Parse(dateString, CultureInfo.CreateSpecificCulture("fr-FR"));



如果没有,请自己创建提供商,或使用 ParseExact [ ^ ],给予适当的格式字符串。


If not, make you own provider, or use ParseExact[^], by giving a proper format string.


这篇关于使用Now()解析日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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