date problem in C# - c#

if i have this in a string : 10/13/2010 8:38:40 AM
how to make it to this: 13/10/2010 08:38:40
thank's in advance

DateTime.ParseExact("10/13/2010 8:38:40 AM","MM/dd/yyyy h:mm:ss tt",CultureInfo.InvariantCulture).ToString("dd/MM/yyyy HH:mm:ss")
edited to make sure 24 hour clock is used in output

Use DateTime.Parse() to convert to a true DateTime object and then use the DateTime.ToString() method to output to the format you desire (code example coming):
var dateTime = DateTime.Parse("10/13/2010 8:38:40 AM");
var formattedString = dateTime.ToString("dd/MM/yyyy HH:mm:ss);

Quick and dirty:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString(new CultureInfo("en-GB"));
Since I know that those formats are for those cultures. However, you can read more about datetime formatting at:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Standard formatting:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Custom formatting:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Or for a more general solution, just pass a format string to DateTime.ToString('formatString'). For example, what you want is DateTime.ToString("dd/MM/yyyy HH:mm:ss"). This allows you to make any format you want.
Example:
DateTime exDT = DateTime.Now;
string exOut = exDT.toString("dd/MM/yyyy HH:mm:ss");
Here's a cheat sheet! You can use ":" where you want it
d Short Date
D Long Date
t Short Time
T Long Time
f Full date and time
F Full date and time (long)
g Default date and time
G Default date and time (long)
M Day / Month
r RFC1123 date
s Sortable date/time
u Universal time, local timezone
Y Month / Year
dd Day
ddd Short Day Name
dddd Full Day Name
hh 2 digit hour
HH 2 digit hour (24 hour)
mm 2 digit minute
MM Month
MMM Short Month name
MMMM Month name
ss seconds
tt AM/PM
yy 2 digit year
yyyy 4 digit year

var strfrom = "10/13/2010 8:38:40 AM";
DateTime dt = DateTime.Parse(strfrom, new CultureInfo("en-US"));
Console.WriteLine(dt.ToString(new CultureInfo("en-GB")));

var curDate = DateTime.Now.ToString() ;
string customDateFormat = Convert.ToDateTime(curDate).ToString("dd/MM/yyyy");

another variant, one one line:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString("dd/MM/yyyy HH:mm:ss");

Related

Parsing Oracle Date to C# datetime

I need to Parse the following string as a DateTime in c#:
"22-FEB-21 09.52.41.256898000 AM"
I've tried the following but it throws exception:
DateTime.ParseExact(
"22-FEB-21 09.52.41.256898000 AM",
"DD-MON-YY HH.mm.ss.ff AM",
CultureInfo.InvariantCulture);
The exception being thrown is
String was not recognized as a valid DateTime.
Try
d-MMM-yy h.m.s.ffffff'000' tt
format string. I.e.
var result = DateTime.ParseExact(
"22-FEB-21 09.52.41.256898000 AM",
"d-MMM-yy h.m.s.ffffff'000' tt",
CultureInfo.InvariantCulture);
Format explained:
d - day in one ore two digit format
MMM - month (abbreviation)
yy - year (two digits)
h - hour 1..12 or 01..12
m - minutes 00..59 or 0..59
s - seconds 00..59 or 0..59
ffffff - fraction of seconds
'000' - 000
tt - AM or PM
There are a couple of problems with your date string.
First, are you sure you are getting 9 decimal places? Since Oracle only supports 6 decimals of precision, maybe you can do something on the query side to remove those?
Also your format string has some problems.
It should be MMM not MON for the month
It should be hh since you are using a 12 hour clock standard (AM/PM) and not 24 hours.
Then you need to fix your fractional seconds, and use a string match for the last two zeros.
Your format string ends up to be:
"d-MMM-yy hh.mm.ss.fffffff'00' tt"
So to test it, you can run this:
var date = DateTime.ParseExact(
"22-FEB-21 09.52.41.258998000 AM",
"d-MMM-yy hh.mm.ss.fffffff'00' tt", CultureInfo.InvariantCulture);
Console.WriteLine(date);
That should solve your problem.

Convert String to DateTime(yyyy-mm-dd)

I'm parsing date from server, date is in this format 6/16/2016 3:15:29 PM Could you help me please convert date to 2016-06-16?
I tried:
DateTime date = DateTime.ParseExact(datestring, "MM/dd/yyyy h-m-s t", System.Globalization.CultureInfo.InvariantCulture);
string formattedDate = date.ToString("yyyy-MM-dd")
but it's giving me error.
You've got 3 problems
You're not using the correct time separators
You're using only one t when you need two
You're using two M when you only need one
Try
DateTime date = DateTime.ParseExact(datestring, "M/d/yyyy h:m:s tt", System.Globalization.CultureInfo.InvariantCulture);
string formattedDate = date.ToString("yyyy-MM-dd");
The reason you need only one M is because MM expects a leading zero. Since the values of the date and time are delimited it's better to use the single versions for month, day, minutes, and seconds because they will work for values with or without leading zeros.
To execute DateTime.ParseExact() format of the input string and the format string must be the same. try this:
DateTime date = DateTime.ParseExact(datestring, "M/dd/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
string formattedDate = date.ToString("yyyy-MM-dd");
These are interesting in the given input string(6/16/2016 3:15:29 PM):
The month is represented in single digit so it should be M instead for MM. We use MM if it is specified as 06.
Same in the case of Hours too. It should be h instead for normal hh
There is a single space in between Date and Time as well as Time and PM.
So we must consider all of these while generating the Format-string for ParseExact

Unable to parse DateTime with a custom format

After reading some other similar questions and trying their suggestions, I'm still unable to get my time to parse into a DateTime-
string time1 = File.ReadAllText(#"C:\Reminders\Reminder1Time.txt");
DateTime dt1 = DateTime.ParseExact(time1, "hh:mm:tt", CultureInfo.InvariantCulture);
dateTimePicker1.Value = dt1;
time1 is a string value of 9:00 AM Other questions have mentioned to use ParseExact to specify a custom format, but it's still not parsing.
The error I get thrown is on the second line
String was not recognized as a valid DateTime.
How would I get the dateTimePicker1 to display the value from the time1 string?
Looks like a stray colon and an extra h if you are expecting a 12 hour clock 1-12 without a leading zero and the AM PM marker with whitespace.
Try: h:mm tt
All of the formatting options are buried in the documentation, here.
var datefromFile = File.ReadAllText(FILELOCATION);
var format = "dd-MM-yyy hh:mm:ss,fff";
var formattedDateTime = DateTime.ParseExact(dateFromFile, format, CultureInfo.InvariantCulture);
Make your format Explicit, remember dd for date capital MM for month and yyyy for year. hh for hour mm for minutes and ss for seconds.
MSDN Formats:
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

Converting string to datetime in C#

There are several threads on similar topic with several solution but none of them are marked as answer.
I have a string and would like to convert into DateTime
string str = "12-3-2013 12-09-10 PM";
DateTime dt = DateTime.ParseExact(str, "MM-dd-yyyy HH-mm-ss tt", new CultureInfo("en-US"));
This gives an error. Is there something to do with CultureInfo? How does CultureInfo affect the output?
dd: The day of the month, from 01 through 31.
d: The day of the month, from 1 through 31.
For sure day is not dd as is using a single digit. Also, you need to change HH by hh or remove am/pm.
Try with this
MM-d-yyyy hh-mm-ss tt
You might need to change MM by M but it's impossible to say with provided example.
string str = "12-3-2013 12-09-10 PM";
Convert.ToDateTime(str);

How can I get this DateTime format in .NET

I'm trying to format some DateTime into this W3C DateTime format :-
Complete date plus hours and minutes:
eg. YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
I originally had this...
var myDateTime = someDateTime.ToString("s",
System.Globalization.CultureInfo.InvariantCulture);
But that results in a string of :
2011-08-31T08:46:00
Can anyone help?
You want "o":
var myDateTime = someDateTime.ToString("o",
System.Globalization.CultureInfo.InvariantCulture);
Use the following:
yourDateTime.ToString( "yyyy-MM-ddTHH:mmK", CultureInfo.InvariantCulture );
Here is more than you'll ever want to know on DateTime formats:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
I believe you want
"yyyy-MM-ddTHH:mmK"
Note:
HH rather than hh to be 24 hour
K to specify the time zone; this relies on the DateTime.Kind being UTC or local; unspecified will end up with an empty string
You should also use CultureInfo.InvariantCulture to make sure no funky culture information is used. (You could quote the - and : as an alternative, but I'd use the invariant culture to make sure.)
You can format it like this:
someDateTime.ToString("yyyy-MM-dd");
Here's the documentation of the 'standard' supported datetime format strings:
http://msdn.microsoft.com/en-us/library/az4se3k1(v=VS.100).aspx
someDateTime.ToUniversalTime().ToString("u");
Will get you pretty close => '2011-09-02 10:22:48Z'. If that isn't good enough, then you can create a custom format string that includes the "T" (see 'Custom Date and Time Format Strings').

Categories