473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Change Date format

37
Hi All,

I want to change the format of date from MM/dd/YYYY to dd/MM/YYYY

like 01/31/2008 to 31/01/2008.

how can i do that.

Thanks in Advance

-John
Aug 27 '08 #1
3 1640
kenobewan
4,871 Expert 4TB
Suggest using globalisation, culture equals en- country code. HTH.
Aug 27 '08 #2
Plater
7,872 Expert 4TB
Or change the .ToString() to match the pattern you want to display as.
Aug 27 '08 #3
balabaster
797 Expert 512MB
Or change the .ToString() to match the pattern you want to display as.
Assuming that the date is in a readily interpretable format... for instance if you display January 7th, 2008 in a numeric format you get 1/7/2008 in the U.S. but in the rest of the world it's 7/1/2008. So really, without knowing what format the string is in to start with which we normally interpret using the System.Globalization.CultureInfo.CurrentCulture (At which point we've done as much as we can to try and automatically verify the input format, at which point as Plater points out, it's as easy as) there's no way to know if it is already in the correct format or not - as you might guess, interpretation of date strings opens the biggest possibility of error.

To input dates you can use a number of options. Once a date is imported into a date object, it is broken into Year, Month and Day properties, the biggest concern is making sure that the right piece of the string makes it into the right property. After this, spitting it out in the right format is a piece of cake:

Expand|Select|Wrap|Line Numbers
  1. 'Grab the regional settings from the current computer
  2.  
  3.  Dim oCulture = System.Globalization.CultureInfo.CurrentCulture
  4.  
  5. 'Force the correct interpretation regardless of culture... 
  6. 'by far the tidiest option, but you rarely get the luxury 
  7. 'of being able to interpret a date so elegantly...
  8.  
  9.  Dim MyDate As Date = New Date(2008, 1, 7)
  10.  
  11. 'Or
  12.  
  13. 'Parse a date string assuming that it's in the format defined 
  14. 'in the 'regional settings on the computer that is running the 
  15. 'software...
  16.  
  17.  Dim MyDate As Date = Date.Parse("1/7/2008", oCulture)
  18.  
  19. 'Or
  20.  
  21. 'Parse a date string explicitly defining the format used to 
  22. 'interpret it and store 'it in the format defined in the regional 
  23. 'settings of the computer that 'is running the software...
  24.  
  25.  Dim MyDate As Date = Date.ParseExact("1/7/2008", "dd/mm/yyyy", oCulture)
  26.  
  27. 'Output date in required format
  28. 'Explicitly define the output format:
  29.  
  30.  Console.Writeline(MyDate.ToString("dd/mm/yyyy"))
  31.  
  32. 'Or
  33.  
  34. 'Output using the format from regional settings
  35.  
  36.  Console.Writeline(MyDate.ToString(oCulture))
Alternatively, if you want to input or output in a different culture's format but don't know what format they use, then you can use the NLS (National Language Support) Language identifiers to specify the output format which will automatically be interpreted by the system:

Expand|Select|Wrap|Line Numbers
  1. Dim oCulture = New System.Globalization.CultureInfo(&H0809) 'UK English
  2. 'Or
  3. 'Dim oCulture = New System.Globalization.CultureInfo("en-GB") 'UK English
  4. Console.Writeline(MyDate.ToString(oCulture))
The following reference may come in handy, it provides a list of the culture/language names and their corresponding identifiers:

http://msdn.microsoft.com/en-us/libr...ltureinfo.aspx
Aug 27 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Laurence Neville | last post by:
This is regarding a change in the Short Date format under Hebrew Regional Settings, that has caused huge problems in our ASP web application. The change appears to have been introduced sometime...
10
by: rob | last post by:
I have a class that among others exposes a string property "Date". The date in this property is stored in the form yyyymmdd. Now I do the following 1) Generate a DataGridViewTextBoxColumn column...
1
by: Tee GEE | last post by:
I included my VBA below. I would like to change the font of the "strBody" to be Bold, italic, color, etc. The code that I attempted to write is identified by ***. Any help would be appreciated....
4
by: Orchid | last post by:
How can I change a Date datatype to a Number datatype? For example, I want a date 10/31/2006 to show 1031 as Number datatype. But I don't want it becomes 39021. What formula should I use? ...
3
by: Bface | last post by:
Hi all, Hope everyone had a good holiday. I am having a difficult time changing the date format of a field from Excel. I have never had this problem before. I link the excel spreadsheet to my DB,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.