How to convert format of a DateTime object to "yyyy-MM-dd" style

Hi all,

i have a date in string data type. i m using Convert.ToDateTime function to convert this string date to DateTime object.

When i get this date as a DateTime object it is in "dd-MM-yyyy" format. but i want to convert its format to "yyyy-MM-dd".

Can anyone tell me how to convert the format of a DateTime object. I don't want the format with string data type.

Please help me.

Thanks in advance.

0
shweta_bhandari
3/18/2009 12:20:37 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

9 Replies
1042 Views

Similar Articles

[PageSpeed] 3

once you got your object just do a .ToString("yyyy-MM-dd") on the end of it.

Otherwise you'll probably have to change your server settings to be that format.


0
Curt_C
3/18/2009 12:26:55 PM

String s; // containt your format

s=s.ToString("yyyy-MM-dd");

Whatever you want make it ...


Regards,
M.Venkatesan
My Blog : http://venkat-dotnetsamples.blogspot.com


Please remember to click “Mark as Answer” on the post that helps you,and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
venkatu2005
3/18/2009 12:36:35 PM

this worked for me.

string datFormat;

datFormat= (e.Row.DataItem as DataRowView).Row["CreatedOn"].ToString();
(e.Row.Cells[4].Controls[1] as Label).Text = Convert.ToDateTime(datFormat).ToString("MM/dd/yyyy hh:MM tt", System.Globalization.CultureInfo.CurrentCulture);
                


SHASHANK BHIDE
CAPGEMINI INDIA
"THE ROAD TO SUCCESS IS ALWAYS UNDER CONSTRUCTION MARK MY RESPONSE AS ANSWER TO HELP ME BUILD IT :)"
0
shashankgwl
3/18/2009 12:45:22 PM

I already mentioned in the blog that i don't want that format with a string data type. Because once it is converted to string it no more a Date. It is just a string looking like a Date but not actually. Bcoz u cannot use the Date operations like compare on it.

That's why i m asking how to convert the format keeping the data type to DateTime only. So that i can use various date operations on it.

 

0
shweta_bhandari
3/18/2009 12:46:57 PM

 hi,

you can use  the following...


Format(Ctype(myDate,Date),"yyyy-MM-dd")

if you like my post mark it as ANSWER

regards,
sunil


Read My Blogs
0
sunilyadav165
3/18/2009 1:04:14 PM

why u people are going for string .. i really don't want a string. i want a DateTime object.

Actually i tried this code. but it is not working as per my expectations. and it is taking the date as "dd-MM-yyyy" its own. i really not getting what to do?

 

 string date = "15-04-2008"; 
 CultureInfo culture = new CultureInfo("ja-JP"); 
//this culture specifies the yyyy/MM/dd format
//Please refer to http://msdn.microsoft.com/en-us/library/ms131008.aspx for more details 

 DateTime startDate = Convert.ToDateTime(date, culture);
 finally i get the startDate in dd-MM only :-(
0
shweta_bhandari
3/18/2009 1:08:11 PM

Hi,

i m not getting the Format keyword in my Visual Studio.

i m using ASP.NET 3.5 with C#

0
shweta_bhandari
3/18/2009 1:12:00 PM

shweta_bhandari:

why u people are going for string .. i really don't want a string. i want a DateTime object.

Because when it's presented in HTML it's TEXT, not an Object anymore anyways.... I'm only suggesting you do the .ToString when you present it in the UI.


0
Curt_C
3/18/2009 8:26:19 PM

Hi all,

i got the solution to change the format of the DateTime object without changing its data type (ie. keeping it as a DateTime object). Here is the solution :-

 

//Add these namespaces
using System;
using System.Globalization;
using System.Threading;

//Actual code converting the frmat


//Create the culture info object to specify the format provider

 CultureInfo culture = new CultureInfo("ja-JP"); 

//Force the system to use the given culture

 Thread.CurrentThread.CurrentCulture = new CultureInfo("ja-JP", true);

 string date = "31-03-2009";
 DateTime startDate = DateTime.ParseExact(date, "dd-MM-yyyy", culture);
The DateTime.ParseExact method solves the actual problem. 
It takes three arguments. First one is the date in string format, 
second on the current format of the date,
 the culture i.e. the output format in which u want the date to be.
u will get more info about culture on msdn. refer the below link
http://msdn.microsoft.com/en-us/library/ms131008.aspx
also get more details about DateTime .Parse method on 
http://msdn.microsoft.com/en-us/library/ms182190.aspx
Hope this will help others. :-)
 

 

0
shweta_bhandari
3/19/2009 7:42:56 AM
Reply:

Similar Artilces:

how can i convert the string from "21/12/2005" which is in dd/MM/yyyy format to MM/dd/yyyy
hi all i would like to convert a string that is when user type in the textbox let say "21/12/2005" in dd/MM/yyy and convert to MM/dd/yyyy . how do it do that ?Karen Dim dt As DateTime = txtInput.Text txtOutput.Text = dt.ToString("MM/dd/yyyy")think this should do it Dim strDate(3) as String Dim strDateNewFormat as String strDate = txtDate.text.split("/"c) strDateNewFormat = strDate(1) & "/" & strDate(0) & "/" & strDate(2) ThanksDinuj Nath Try this link for sample code.  Hope this helps.   http://msdn.microsoft.com/library/default.asp?url=/libr...

MaskedEditExtender date format problem ("yyyy-MM-dd" with cultureName="sv-SE" doesn't work)
Hi All, I need to create MaskedEditExtender for Sweden date which uses format "yyyy-MM-dd". I have the following code below. CalendarExtender doesn't work with current MaskedEditExtender. Also the validation doesn't work properly. <asp:TextBox ID="txtFSFV" MaxLength="100" style="width:70px" runat="server" /> <asp:HyperLink ID="hplGetCalendar" NavigateUrl="javascript:void(null)" runat="server"> <img src="~/images/calendar.png" runat...

Calendar extender format="dd.MM.yyyy"?
 Hi. I have one question about date format in ajax toolkit calendar extender. I want to set the format to "dd.MM.yyyy" because thats the format I need to insert into database. Here is my test code: <%@ Page Language="C#" MasterPageFile="~/master.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="test" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><asp:Content ID="Content1" ...

How to Convert "string" to "DateTime"
hi , i have a problem i have an textarea that i want to convert to DateTime format (dd/MM/yyyy) . the data in the textarea is (dd/MM/yyyy for example 21/12/2005). i need it to add this data in sql server , in smalldatetime formation colum . plz help.     Wouldn't you just use: DateTime dt1 = DateTime.Parse( stringToParse, formatProvider, styles) This doesn't quite seem like a SQL Server question ... but in ASP.NET you can convert a string to a date using the DateTime.Parse(string) method. Then you could use this as the value of a parameter to a stored procedure with t...

format Date field in Dataset to "mm/dd/yyyy"
Currently I have a query that is populating a dataset with a date value. Here is my code:  With cmd .CommandText = "SELECT pEff_Dt FROM MyTable WHERE condition1 = condition2" .Parameters.Add(BMItemno) .Connection = SQLConn .CommandType = CommandType.Text End With adapter.SelectCommand = cmd adapter.Fill(dsEffDate) How can I access the only date field passed to my dataset (pEff_Dt) and format it to "mm/dd/yyyy"? Currently is formated to Sat Sep 30 16:31:33 PDT 2006 Any suggestions?  Victor A. Pa...

how to get date in "dd/mm/yyyy" using C#.net?
Hi I have  textbox, while page loads, by default this texbox need to be with today's date in "dd/mm/yyyy"  format... Can anyone suggest how to do this using c#.net. Thanks aviThanks and RegardsAvinashavicool08@gmail.com  <body>    <form id="form1" runat="server">    <div>    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    </div>            </form></body...

How to validate date time format must be "yyyy/mm/dd"?
I use VS2003 , C# In my Layout have a TextBox for user to input Publish Datethe date time format must as "yyyy/mm/dd"layout like belowPublish Date | TextBox |   ex:2008/09/22but sometimes user just input 20080922 or 97/09/22 and so on...how to validate this TextBox must as "yyyy/mm/dd"?thank you very much you can accept the date as long as it is valid, and then convert it to whatever format you want. Here are 2 options to validate Date 1) The CompareValidator is an option, use Operator=DataTypeCheck and Type=Date for Validator 2) RegularExpression...

How to display "dd-mm-yyyy" format from database to text box ?
Hi How to display "dd-mm-yyyy" format from database to text box ? i have written like this but i m not getting the expected op. txtDate.Text= string.Format(ds.Tables[0].Rows[0]["NewsDate"].ToString(),"dd-mm-yyyy");Chandra Dev go through this.... http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspxChinna_sv... Try like this: txtDate.Text= ds.Tables[0].Rows[0]["NewsDate"].ToString("dd-MM-yyyy"); By the way, I advise that you check for nulls, prior to doing this, just to be safe (especially is NewsDate fields can be null).C# ...

DateTime Editmask "MMM DD YYYY HH:MM AM/PM"
Application was in PB 6.5. Ported to PB8 Build 8004, SQL 2000 backend. Have a column in datawindow (type=datetime) with the following Datetime editmask MM/DD/YYYY HH:MM AM/PM with spin property disabled. Cannot edit or enter the date. After typing the date, on pressing Tab key or enter key, the date column displays null date. Problem arises because of AM/PM format. When I remove AM/PM format it works fine. I do not want to move to 24 hour format(MMM DD YYYY HH:MM) though that works fine. I've read of similar problems in the newsgroup, but haven't yet seen a solutio...

DateTime Editmask "MMM DD YYYY HH:MM AM/PM" #2
How to add a DateTime Editmask "MMM DD YYYY HH:MM AM/PM" (with spin control) Our application is in PB 6.5. We are having columns in Datewindow objects withthe following Datetime mask MMM DD YYYY HH:MM AM/PM with spin property enabled This will allow the user to enter the date When we upgraded the application into PowerBuilder 8.0. We are not able to enter the date. After typing the date, on pressing Tab key or enter key, the date column displays null date. Problem arises because of AM/PM format. When we remove AM/PM format it is working fine. Moreover, we dont ...

Problem in Date Comparison with Compare Validator in "dd/MM/yyyy" format
I want to compare dates  for that I have taken two textboxes in which dates are coming through ajax calendar in "dd/MM/yyyy" format I am using compare validator  for comparing with condition that value in textbox2 should be greater than textbox 1  in textbox1 if I enter date suppose"10/03/2008"  and in textbox 2  "11/03/2008" it works fine But in textbox1 if I enter "20/02/2008" and in textbox 2 "11/03/2008" it doesn't works and give message "value in textbox2 should be greater than textbox 1&quo...

How To Convert dd/mm/yyyy date into mm/dd/yyyy format
Hi My Application recieves date in dd/mm/yyyy format.But I want to convert into dd/mm/yyyy format.If is there any format for that please help me.Debabrata Pattanaik DateTime datenow = DateTime.Now;DateTime convertdate = Convert.ToDateTime(datenow.ToString("mm/dd/yyyy"));Regards,Vinz"Code, Beer and Music" that's my way of being a programmer!How to get your Forum Question Answered | Blog | CodeASP.NET now.toString("mm/dd/yyyy")  Hi ,Another way is :-   now.tostring("d");Go to page and change culture property of the page to en-us.Not b...

Convert Date Format from mm/dd/yyyy to dd/mm/yyyy
Hi,In my Web application I need to show the date in dd/mm/yyyy format to the user. In my application i am showing the date in various forms e.g. In GridView, In dataGrid, In combo, In textBox etc.how will it be possible? ThanksPriyank Kansal  dear,try this, String.Format("{0:yyyy-MM-dd HH:mm:ss}", Now)it will return string like "2007-05-01 12:30:01" set format as per your need.  regards,Niraj sikotara.<!-- Search before you scramble. --!> actually pretty, I am using hundreds of pages in my web application. Now it is very difficult to change the format of ea...

"STYLE" property in "External User Object"
Hi all,... Does anyone know document to explain "STYLE" property in "External User Object" Thanks for helping... O s a m a D i a b diab98@hotmail.com I suspect that would depend on which EXTERNAL user object it is. Check with the vendor. Osama Diab wrote in message ... >Hi all,... > >Does anyone know document to explain "STYLE" property in "External User >Object" > >Thanks for helping... >O s a m a D i a b >diab98@hotmail.com > > > > I want any document, help please!! Je...

Web resources about - How to convert format of a DateTime object to "yyyy-MM-dd" style - asp.net.web-forms

Resources last updated: 12/4/2015 12:56:27 PM