How do I keep the 0's in a Date - c#

I am trying to figure out how it is that I can keep the 0's or add them when I grab a date.
What Im getting is this:
6/15/2010
What I'm tring to get is:
06/15/2010
I have added it so that it checks the length to and if its less than 6 (im stripping the "/") it pads the left side. That solves the issue when the month is a single digit, but what about when the date is a single digit.
My ultimate goal is to have a date such as:
1/1/2010
read out like:
01/01/2010
Any suggestions would be greatly appreciated.

Use a custom format : dd/MM/yyyy, or in your case MM/dd/yyyy. Note the capital M, the small m gets you the minutes.
string s = DateTime.Now.ToString("MM/dd/yyyy");

You need to use a custom DateTime format string:
string str = someDate.ToString("dd/MM/yyyy");

It depends on the format of date you are using.
For instance, dd/MM/yyyy will produce 01/05/2009 and d/M/yyyy would produce 1/5/2009
A complete reference can be found there : http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

You want something like this:
string myDate = "1/1/2010";
DateTime date = DateTime.Parse(myDate);
string formattedDate = date.ToString("MM/dd/yyyy");
If the starting date is some other unrecognized format you could use DateTime.ParseExact();

Use DateTime.ParseExact() to parse the string into a valid datetime object and then use DateTime.ToString("dd/MM/yyyy") to get result in desired format.

Related

How can i change a date string format

I'm returning from Facebook Graph a friend birthday in MM/DD/YYY format. I need to show it in DD/MM/YYY, cause it's the brazil date format. I'am returnin a M/D string. I cant change the datetime format. How can I only display D/M for the user without changing datetime? Only manipulating the returning string?
The error that you're receiving tells me that your birthday variable is a string. To format it, you can first convert it to a DateTime (it's probably the simplest way).
var birthdayString = "06/28/2013";
var date = Convert.ToDateTime(birthdayString);
date.ToString("dd/MM - MMMM d"); // 28/06 - June 28
date.ToString("dd/MM/yyyy"); // 28/06/2013
Like I said in the comments above, you can check out more about formatting dates here:
Custom Date and Time Format Strings
Depending on the format just take your date to string.
birthdaydate.ToString("dd/MM/YYYY"); //10/01/1970
birthdaydate.ToString("dd MMMM"); // 10 January
birthdaydate.ToString("MM/dd/YYYY"); //01/10/1970
and so on. please refer to http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx for further information
If you don't want to input the format manually, you can do this instead:
string dateString = myDateTime.ToString(new CultureInfo("pt-BR"));

SQL datetime to C# string and back to SQL datetime

I have a webservice method that gets data from sql of the format
2012-11-18 11:21:03 when i save it to C# string it becomes this format: 18.11.2012 11:21:03
How do i change it back to the SQL format 2012-11-18 11:21:03 ?
Parse it into a dateTime again
DateTime myTime = DateTime.Parse(myString);
and back into a proper to string
myTime.ToString("yyyy-MM-dd HH:mm:ss");
Or just read it into a datetime and cut out the middleman.
You can get the universally sortable string format (which looks like the one used by SQL server) by using the format string "u" like this:
var dateTimeString = String.Format("{0:u}", yourDateTime);
Simply run the below code,
var newDateTime = oldDateTime.Date.ToString("yyyy-MM-dd HH:mm:ss");
Its just converting it back to the SQL Format DATETIME
Trouble with Dates as strings is they are ambiguous and the formats can vary based on where you are in the world, or even local machine settings. You might assume a date string is yyyy-mm-dd but what if it is actually yyyy-dd-mm? Some dates will appear to work and some will be invalid.
In other words is 2013-02-10 the 10th of February or is it the 2nd of October? If it is just a string you have no way of knowing for sure what was intended.
Your best bet as suggested by #Haedrian is to store in a DateTime C# object, not a string. That way it is never ambiguous and you have access to various date specific functions. If you must store as a string you can convert back to a date as above or use
DateTime.TryParse(datestring, out dateVariable);
which won't throw an exception for an invalid format. Depends if you want exceptions!
Also I would suggest if you must use strings to use a 3 character month in strings, which again eliminates the ambiguity, e.g.
"dd-MMM-yy hh:mm tt"

DateTime formats C#

I have a string which needs to be converted and validated to a DateTime. The string is in the following format 'dd.mm.yy'
I am trying to convert it to DateTime using the following
string format = "dd.mm.yy";
date = DateTime.ParseExact(current.Substring(aiRule.AiLength), format,
CultureInfo.InvariantCulture);
but unfortunately this fails.
The question is how to convert a string in the format 'dd.mm.yy' to a DateTime ?
Thank you
mm means "minutes". I suspect you want "dd.MM.yy". See MSDN for more information about custom date and time format strings.
(In particular, read the part about the "yy" specifier and how it chooses which century to use. If you can possibly change the input to use a four digit year, that could save you some problems...)
the string format should be like this....
string Format = "dd.MM.yy"
mm is for showing minutes
MM is for showing months..
I hope it will helps you...
As earlier posts has already pointed out, mm means minutes and MM means months. I ran this test snippet and it works as expected:
string format = "dd.MM.yy";
string date = "27.10.11";
DateTime result;
result = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture);
I'll tell something "heretical". If dd.MM.yy (with 2 or 4 yy) is the format of your local culture, then you could let the DateTime.Parse (not ParseExact!) do its work without setting it to CultureInfo.InvariantCulture, or perhaps setting it to your local culture like new CultureInfo("it-IT").

parse iCalendar date using c#

Is there a direct way to parse an iCalendar date to .net using c#?
An iCalendar date looks like this:
2009-08-11T10:00+05:0000
I need to parse it to display it in a friendly format... thanks
string strDate = "2009-08-11T10:00+05:0000";
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.FullDateTimePattern = "yyyy-MM-ddTHH:mmzzz";
DateTime dt = DateTime.Parse(c.Substring(0, c.Length-2), dtfi);
zzz is for time zone, but is only recognized when expressed like this: +xx:xx.
I tested with your example, removing the last 2 0's then parsing with a custom DateTimeFormatInfo works.
You can use DateTime.Parse() to parse everything before the +. I do not know the iCalendar format specification but I assume after the + is the hours/minutes to add to the date before the +. So you could then use AddHours() and AddMinutes() to add the required bits to the DateTime returned by DateTime.Parse().
This requires a bit of string parsing but with a bit of regex you should be fine...
Since this is not a standard format string, but you know the exact format, you can use DateTime.ParseExact and specify a custom format string, like this:
DateTime.ParseExact(d, "yyyy-MM-ddTHH:mmzzz00", CultureInfo.InvariantCulture);
The 'zzz' specifier represents the hours and minutes offset from UTC, and the two concluding zeros are just literals to match format with which you're dealing.

DateTime.ParseExact string format exception

I am trying to convert a string into datetime with the following C# code,
DateTime dTo = DateTime.ParseExact(dateTo, "mm/dd/yyyy", CultureInfo.InvariantCulture);
eachtime I pass dateTo as 1/1/2010 it fails, instead it needs the string to be 01/01/2010.
What string format should I use to support both 01/01/2010 and 1/1/2010?
Using the following date format expression will allow you to use either single or double digit day and month elements.
"M/d/yyyy"
Note that the capital M is significant - a lower case m is the placeholder for minutes.
You will find more information related to date format strings here.
You can use the following Powershell command to test them.
[DateTime]::ParseExact('01/01/2010', 'M/d/yyyy', $null)
Capital M is month, little m is mins i think.
But to the point of the question, use Parse. ParseExact implies you know the exact format of the input.
You could try this format: MM/dd/yyyy, but I think there's no single format string that could support both inputs. You could test if the length of your dateTo string is less than 10 characters use M/d/yyyy, otherwise MM/dd/yyyy.

Categories