开发者

stop vb.net from turning 02/02/2011 to 2/2/2011

开发者 https://www.devze.com 2023-02-08 21:59 出处:网络
For some reason vb keeps changing my dates format and removes the 0s at first this wasnt a problem but it really messes up my sorting in my gridview.

For some reason vb keeps changing my dates format and removes the 0s at first this wasnt a problem but it really messes up my sorting in my gridview.

Dim aftersubtraction As Date
aftersubtraction = departuredate.AddDays(-dates1.Text)
dates.Add(aftersubtraction.A开发者_高级运维ddDays(-gracep.Text))


DateTime objects store their variables as integers. It will always engage in this behavior. To prevent it, you need to format the date as a string of your choosing before you send it where-ever you send it:

aftersubtraction.ToString("MM/dd/yyyy")


Don't confuse how a date is stored internally (doesn't really matter), to how you display it.

"Feb 2nd 2011" is the same date as "02/02/2011" or "2/2/11".

When you have a date variable and want to display it a certain way, you should use the correct format string - either a custom or a standard one.

In your case you seem to want this:

Dim outputDate as String = myDate.ToString("dd/MM/yyyy") ' European/UK style
Dim outputDate as String = myDate.ToString("MM/dd/yyyy") ' US style


Try this:

.ToString("MM/dd/yyyy")


In addition to what Joel said - you shouldn't be sorting on the string representation of a date object - you should be sorting on the date value itself, then it won't matter how it's sorted.


Change your Region and Language settings to your correct culture this will probably solve your issue, but if you have problems with sorting dates in a gridview you might be passing a datasource that stores the date as a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号