Click here to Skip to main content
15,038,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I a working on VB.NET windows based application. I need to set default date format in my application.

Suppose date format in Operating System for Short date is "dd.MM.yyyy"

But i want "M/d/yyyy" format for my application. How do i convert to "M/d/yyyy" at run time.
Posted
Updated 11-Feb-20 20:45pm

you can change system's date format runtime using this code
(will change format in regional language settings)
VB
Imports System.Threading

Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False)

Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\International", "sShortDate", "M/d/yyyy")

Happy Coding!
:)
   
Comments
Member 11268628 17-Jul-20 13:03pm
   
Thank you very much for the solution. I've implemented it inside one of my projects
Hi,

You can simply use below code,

C#
DateTime currentDate = DateTime.Now;
var dateResult = currentDate.ToString("M/d/yyyy");


Hope this will help you,

Thanks
-Amit Gajjar
   
Comments
kals84 26-Jul-12 6:43am
   
this code is not working :(
AmitGajjar 26-Jul-12 7:06am
   
can you post your code ? what error you have ?
VB
Protected Sub Application_BeginRequest(sender As [Object], e As EventArgs)
	Dim newCulture As CultureInfo = DirectCast(System.Threading.Thread.CurrentThread.CurrentCulture.Clone(), CultureInfo)
	newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy"
	newCulture.DateTimeFormat.DateSeparator = "-"
	Thread.CurrentThread.CurrentCulture = newCulture
End Sub
   
Comments
Member 12254178 7-Feb-16 11:00am
   
Hi, I am placing this code in code window. but its not affecting.
Actually I am following a date format like "dd/MM/yyyy" in my application using vb.net 2010 express. But system date formats has different for each system. So I need to set my date format globally for my application (Not to change system date settings. Only for my application). It should not influence with any system date format. Can you please explain the step procedure.
Thank you.
If you are using the DateTimePicker1 then you can set the DateTimePicker1 properties as Custom as like you "M/d/yyyy" and format is "Custom" it is simple method.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900