Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
170 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...