C# .NET - Convert Date in dd/mm/yyyy into dd/mmm/yyyy format

Asked By Harbans Singh Sidhu on 06-Nov-09 04:08 AM
Hi All,

I am accepting date time from user and its come in dd/mm/yyyy format in ASP.Net and C#, I want to send this date in dd/mmm/yyyy format to MSSQL Server.

Please suggest, how can I get it.

Regards,
Harbans Singh Sidhu


Re - Huggy Bear replied to Harbans Singh Sidhu on 06-Nov-09 04:10 AM

Convert the TextBox string to DateTime first


DateTime inputDate = Convert.ToDateTime(TextBox1.Text);

While passing it to the DB format it as shown

inputDate.ToString("dd/MMM/yyyy");

This will do the trick.

why u use mmm? - Web Star replied to Harbans Singh Sidhu on 06-Nov-09 04:34 AM

why u want to month as mmm , where as month is always in only mm two value in any format of datetime in C#.net as well as sql server

u can use any of these format in sql server

CONVERT function is used to change or convert the DateTime formats.By using convert function you can get only Date part or only Time part from the datetime.

CONVERT Function Syntax: CONVERT(data_type,expression,date Format style)

Let's take Sql Server DateTtime styles example:

Format

Query

USA mm/dd/yy

select convert(varchar, getdate(), 1)

ANSI yy.mm.dd

select convert(varchar, getdate(), 2)

British/French dd/mm/yy

select convert(varchar, getdate(), 3)

German dd.mm.yy

select convert(varchar, getdate(), 4)

Italian dd-mm-yy

select convert(varchar, getdate(), 5)

dd mon yy

select convert(varchar, getdate(), 6)

Mon dd, yy

select convert(varchar, getdate(), 7)

USA mm-dd-yy

select convert(varchar, getdate(), 10)

JAPAN yy/mm/dd

select convert(varchar, getdate(), 11)

ISO yymmdd

select convert(varchar, getdate(), 12)

mon dd yyyy hh:miAM (or PM)

select convert(varchar, getdate(), 100)

mm/dd/yyyy

select convert(varchar, getdate(), 101)

yyyy.mm.dd

select convert(varchar, getdate(), 102)

dd/mm/yyyy

select convert(varchar, getdate(), 103)

dd.mm.yyyy

select convert(varchar, getdate(), 104)

dd-mm-yyyy

select convert(varchar, getdate(), 105)

dd mon yyyy

select convert(varchar, getdate(), 106)

Mon dd, yyyy

select convert(varchar, getdate(), 107)

hh:mm:ss

select convert(varchar, getdate(), 108)

Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM)

select convert(varchar, getdate(), 109)

mm-dd-yyyy

select convert(varchar, getdate(), 110)

yyyy/mm/dd

select convert(varchar, getdate(), 111)

yyyymmdd

select convert(varchar, getdate(), 112)

Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h)

select convert(varchar, getdate(), 113) or select convert(varchar, getdate(), 13)

hh:mi:ss:mmm(24h)

select convert(varchar, getdate(), 114)

Reply - Harbans Singh Sidhu replied to Web Star on 06-Nov-09 04:49 AM

If you use format dd-mmm-yyyy date format while send you value to sql server this will not make any impact that which date format has been used by sql server, like if you send 12/10/2009 as 12th Oct SQL Server may count it as 10 Dec while there is no any kind of ambiguty if you send date format in dd-mmm-yyyy this is best practice.

Please suggest if you have solution on Dot Net end. Else Ram Jee Kee
Date ToString - F Cali replied to Harbans Singh Sidhu on 06-Nov-09 06:19 AM

Aside from the dd-mmm-yyyy format, you can also use the yyyy-mm-dd format since there's no yyyy-dd-mm format.  To convert your date to dd-mmm-yyyy format, you can do the following (assuming your field is a DateTime type already):

DateTime todaysDate = System.DateTime.Now();

string newDate = todaysDate.ToString("dd-MMM-yyyy");

Regards,
http://www.sql-server-helper.com/sql-server-2008/index.aspx