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?



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 DataType配置为C#。我的意思是,“控制面板”中配置的“dd / MM / yyyy”。




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



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

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


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

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

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



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

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


  Dim dd As DateTime = DateTime.Now 



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

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

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


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

微信公众号搜索 “ IT屋 ” ,选择关注