vb.net日期格式与c#日期时间格式(vb.net date formatting vs c# datetime formatting)

其他开发 IT屋
问 题

I am doing an Visual Studio vb.net WinForm App.

I am used to work with c#. In C# Datetime DataType I have configured as "dd/MM/yyyy" as is configured in Control Panel and Regedit.

In VB.Net, is different, when I create a Date variable like this

Dim var As New Date
var = DateTime.Now

Date.Now returns format "mm/dd/yyyy".

So every date format a assign to a variable, VB.Net automatically convert to format "mm/dd/yyyy".

I need Date DataType in VB.Net to be configured as C#. I mean, "dd/MM/yyyy" as it is configured in Control Panel.

Is that possible?

Thanks

解决方案

I want to work in format dd/MM/yyyy

Actually, you don't. You want to work with dates as DateTime types, because it's most flexible. Storing and working with dates as another type (such as a string in a particular format) in inherently less flexible and more error prone than using the proper type.

This is a perceptual problem; you think about dates in a certain way, and you seem to be demanding the computer represent them in YOUR way, but that's the flaw; you need to work with dates in the way .net does it because it's the best way.

In the same way that you wouldn't think of storing/working your numbers in a string, because "2" + "2" = "22", whereas 2 + 2 = 4, you should store, manipulate and process dates are DateTimes. If you want a particular representation just as the date data is being displayed on screen, THEN you can format it into MM/dd/yy for your american friends, or dd-MM-yyyy for your Europeans

dd= 16 +'/'+ 10 +'/'+ 17 It should assign 16/10/2017, but it assign 10/16/2017..

Other people have covered this, but whatever class that Date is, dump it, and use a built in DateTime, maybe like this:

DateTime d = new DateTime(2017, 10, 16);

DateTime has a constructor that takes a year, month and day. Once you have your datetime object you can manipulate it with the AddXXX methods to add 10 days or subtract 15 hours etc. You can do onedate - anotherdate and get a TimeSpan object that will tell you how many minutes, hours, etc are between the two dates.. And so on

But it need it to define in General to all application. I need that Date type defined as dd/MM/yyyy by default.

You can't. .NET will never think of your date in any way other than the way it does internally, it will never process it in any way other than it's programmed to do. You say you know how to format it on the way out to a report.. and you seem to be saying you want to specify that format globally so that every time you, or anyone looks at a date, it's that format. You must appreciate that .net will still treat dates as the way it does internally. You can and should only influence the part of the process that puts the date on screen in some visible form for the user to read.. But really, if you're writing an application that will be used by other people, you shouldn't override the way their system treats dates, within your app..

You should instead just work with dates as dates, .ToString() them onto screen when you need to and let the thread, that is dealing with the conversion, use whatever culture specific settings it picked up from the user's operating system. This way your app uses dates as dates, and the american using your app sees 10/12/2017 and knows it's the 12th oct, and the european using your app sees 12-10-2017 and knows the same

If you want to force a particular date format during development, set your windows culture settings to the formatting you want. Don't hard code a date format into your app

If I ask for DateTime.Now ,it returns a "MM/dd/yyyy", I need to retrieve a format "dd/MM/YYYY"

No, it doesn't. Datetime.Now returns you a DateTime object of a snapshot of the computer's clock at the time it was called. It represents the date internally as a number, not a string, not in any format etc. When you MessageBox.Show it (or whatever) it's converted to a string text representation and that's what you're seeing as "MM/dd/yyyy" but sure as heck DateTime.Now is not returning you a string like "10/12/2017"

In Main() I add

    Dim dd As DateTime = DateTime.Now

Soo.. Just run me through the logic of the part where you tagged this question as C#, said it was C# and just now you've dumped a load of VB.NET from "your program" into it?

I want a global setting so I get "dd/MM/YYYY" in all my Application, so I do not need to Parse or Cast every time I need it....

Really, think on what I've said about how .NET treats dates. You stores dates as DateTime instances. When you get user input and create user output, those are really the only times you might need to convert from a string representation to a date, but on the input side of things there are controls such as DatePicker that allow the user to choose a date and time, and give you the value they chose, as a DateTime - no strings involved. On the output side; leave it - let the dates display in whatever format the user (including you) has picked in his Windows Region control panel

If you're reading in a text file with dates in a certain format, that's when you'd use DateTime.ParseExact(), to convert that text representation, into a DAteTime.. But you store it in your app memory, in xml files, in database columns etc, as a proper DateTime (or equivalent) datatype, NOT a string

The problem here is not with how .NET thinks about dates, it's with how you're thinking about them/how you think .net thinks about them. Once you're over that part, it'll get much easier

本文地址:IT屋 » vb.net date formatting vs c# datetime formatting

问 题

我正在做一个Visual Studio vb.net WinForm应用程序。

我习惯使用c#。在C#日期时间数据类型我已经配置为“dd / MM / yyyy”在控制面板和注册表中配置。

在VB.Net,是不同的,当我创建这样的日期变量





Date.Now

“。

我需要VB.Net中的Date DataType配置为C#。我的意思是,“控制面板”中配置的“dd / MM / yyyy”。



这可能吗?

谢谢


解决方案

我想以dd / MM / yyyy的格式工作




其实,你没有。你想使用DateTime类型的日期,因为它是最灵活的。存储和使用日期作为另一种类型(如特定格式的字符串),本质上不太灵活,并且比使用正确类型更容易出错。



这是一个知觉问题;你以某种方式考虑日期,而且你似乎要求计算机用你的方式代表他们,但那是缺点;你需要使用日期的方式.net是这样做的,因为这是最好的方式。



以同样的方式,你不会想到存储/处理你的数字在一个字符串中,因为“2”+“2”=“22”,而2 + 2 = 4,则应该存储,处理和处理日期为日期时间。如果您想在屏幕上显示日期数据时需要特定表示形式,那么可以将其格式化为美国朋友的MM / dd / yy格式,或输入欧洲人的格式为dd-MM-yyyy




dd = 16 +'/'+ 10 +'/'+ 17
应该分配16/10/2017,但分配10/16/2017 ..



其他人已经覆盖了这个,但无论日期是什么类,转储它,并使用内置的DateTime,也许像这个:

  DateTime d = new DateTime(2017,10,16); 


DateTime有一个构造函数,需要年,月和日。一旦你有你的日期时间对象,你可以使用AddXXX方法来操作它来添加10天或减去15小时等。你可以做onedate - anotherdate并获得一个TimeSpan对象,告诉你有多少分钟,小时等日期..等等。

lockquote
但它需要它来定义一般的所有应用程序。我需要默认的日期类型定义为dd / MM / yyyy。




你不能。除了内部的方式,.NET永远不会考虑你的日期,除了它被编程之外,它绝不会以任何方式处理它。你说你知道如何在报告的出路上对其进行格式化,而你似乎在说要全局指定该格式,以便每当你或任何人查看日期时,就是这种格式。你必须明白,.net仍然会把日期当作内部的方式处理。您可以也应该只影响将日期放在屏幕上的部分,以便用户阅读。但实际上,如果您正在编写将被其他人使用的应用程序,那么您不应该在你的应用中重写他们的系统处理日期的方式。



你应该使用日期作为日期,.ToString()当你需要的时候,让线程(即处理转换)使用从用户的操作系统中获取的任何文化特定的设置。这样,你的应用程序使用日期作为日期,美国人使用你的应用程序看到10/12/2017,知道它是十二月十八日,欧洲使用你的应用程序看到12-10-2017,并知道相同的



如果您想在开发期间强制使用特定的日期格式,请将Windows文化设置设置为所需的格式。不要将日期格式硬编码到您的应用程序中如果我要求DateTime.Now,它将返回一个“MM / dd / yyyy “,我需要检索格式”dd / MM / YYYY“


不,不。 Datetime.Now返回一个计算机时钟快照的DateTime对象。它表示日期在内部作为一个数字,而不是一个字符串,而不是任何格式等。当你MessageBox.Show它(或其他),它被转换为一个字符串文本表示,这就是你所看到的“MM / dd / yyyy “但肯定是因为DateTime.Now没有返回给你一个字符串,如”10/12/2017“
$ b


在Main()



  Dim dd As DateTime = DateTime.Now 



只要运行一下你标记为C#的部分的逻辑,就是C#,刚才你已经倾倒了VB.NET从“你的程序”加载到它吗?

lockquote

我想要一个全局设置,所以我得到“dd / MM / YYYY “在我所有的应用程序,所以我不需要解析或投射每次我需要它....


真的,想想关于我如何处理日期。您将日期存储为DateTime实例。当你得到用户输入并创建用户输出时,这些实际上是你可能需要从字符串表示转换为日期的唯一时间,但是在事物的输入端有诸如DatePicker之类的控件允许用户选择日期和时间,并给你所选择的价值,作为日期时间 - 不涉及任何字符串。在输出方面;离开它 - 让日期显示在用户(包括你)在他的Windows区域控制面板中选择的任何格式



如果您正在阅读包含日期的文本文件在某种格式,这就是当你使用DateTime.ParseExact(),将该文本表示,转换为一个DAteTime ..但是,您将它存储在您的应用程序内存,XML文件,数据库列等作为一个适当的DateTime (或同等)的数据类型,而不是一个字符串



这个问题不在于.NET如何考虑日期,而在于你如何思考他们/你的想法.net认为他们。一旦你完成了这个部分,它会变得更容易


本文地址:IT屋 » vb.net日期格式与c#日期时间格式