This could be quite a straightforward question, I've tried find a answer but couldn't.
What I'm trying to do is to format my DateTimeOffset to use format specifier "G" and append timezone "zzz" with it. I like to use 'CurrentCulture' as well.
myDateTimeOffset.ToString("G zzz", CultureInfo.CurrentCulture)
However, I'm getting result like 'G +12:00'.
I'm expecting to get like '28/07/2016 3:36:31 PM +12'.
Any advice to get it to format properly? Thanks.
try the following:
string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:G} {0:zzz}", myDateTimeOffset);
Related
I was looking in the Microsoft doc's and I can't find any explanation why ParseExact doesn't understand my date.
Could somebody explain why this code throws an exception?
DateTime.ParseExact("6092019", "dMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None)
From the docs: DateTime.ParseExact Method
If format is a custom format pattern that does not include date or
time separators (such as "yyyyMMddHHmm"), use the invariant culture
for the provider parameter and the widest form of each custom format
specifier. For example, if you want to specify hours in the format
pattern, specify the wider form, "HH", instead of the narrower form,
"H".
So in your case you probably should use approach suggested in John's answer - add "missing" zero and parse with wider date format "dd"
The problem here seems to be that d can be a one-digit or two-digit date, so the parser struggles to determine if "2102019" refers to the 2nd of November 2019, or the 21st of... and then it breaks. With delimiters, the parser is able to act more intelligently. It will happily parse "2-10-2019" using "d-MM-yyyy".
My suggested solution to your problem is to pad the string, and change your format string:
string dateToParse = "6092019";
string paddedDateToParse = dateToParse?.PadLeft(8, '0'); // 06092019
DateTime parsedDate = DateTime.ParseExact(paddedDateToParse, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Try it online
I'm getting a DateTimeOffset string as "2018-10-16T193850+0200", but I think it's none of the standard formats. Mainly, the "+0200" part is not standard, because it lacks the colon.
What format do I have to specify to parse DateTimeOffsets like this? thank you!
You can use ParseExact:
DateTimeOffset offsetDate = DateTimeOffset.ParseExact(
"2018-10-16T193850+0200",
"yyyy-MM-dd'T'HHmmsszzzz",
DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.None);
Read: Custom Date and Time Format Strings
Unfortunately setting DateTimeInfo.TimeSeparator to empty string won't help.
But you can use DateTimeOffset.ParseExact as follows:
DateTimeOffset date = DateTimeOffset.ParseExact("2018-10-16T193850+0200", "yyyy'-'MM'-'dd'T'HH''mm''ss''K", CultureInfo.InvariantCulture.DateTimeFormat);
The trick is to specify 'K' format specifier in order to accept all kinds of offsets. Additionally to the above date string the following will be parsed correctly as well:
"2018-10-16T193850Z" - UTC
"2018-10-16T193850" - local time
EDIT
My answer is similar to Tim Schmelter's one, except by the 'K' part.
everyone.
I have a datetime coming back from a music file in the format:
32510
Maybe, it means 1989/01/02
How can i get the datetime.parse function to pick up on this? Ie parse it without erroring? Cheers
This DateTime(32510) is in double so we can't simply use datetime.parse to convert 32510 to DateTime. To convert a double to date we need to use DateTime.FromOADate method.
For more details you can go through this MSDN link:
https://msdn.microsoft.com/en-us/library/system.datetime.fromoadate(v=vs.110).aspx
Quite often interop dates are stored as integers, so you'll need to convert them to actual dates. See below as an example:
var dt = DateTime.FromOADate(32510);
Console.WriteLine(dt);
Where the output is:
2/01/1989 12:00:00 AM
I don't think you can use DateTime.Parse() to convert 32510 to 1989/01/02 though.
It sounds me like Ole Date, try using DateTime.FromOADate
DateTime date = DateTime.FromOADate(32510)
//output - 1/2/1989 12:00:00 AM
I need to format a date to the following format:
M-d-yyyy
I tried using:
string.Format("{0:M-d-yyyy}", DateTime.Now)
But the output string will depend on the CurrentCulture on the computer where it's run, so sometimes the output might be 07/09/2014 or 07.09.2014 instead of 09-07-2014.
How can I easily prevent it from converting it based on the culture and treating it as a literal string?
Use CultureInfo.InvariantCulture as the culture or provider argument.
String.Format(CultureInfo.InvariantCulture, "{0:M-d-yyyy}", DateTime.Now)
Use CultureInfo.InvariantCulture as an IFormatProvider parameter:
DateTime.Now.ToString("M-d-yyyy", CultureInfo.InvariantCulture);
You can set the culture of your program with this:
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;`
You can also use a specific culture if you want (I think en-US is the one you need)
Use the following:
DateTime.Now.ToString("d", DateTimeFormatInfo.InvariantInfo);
or apply other formatting specs as detailed in http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx
Pertinent to your case it could be written as:
DateTime.Now.ToString("M-d-yyyy", DateTimeFormatInfo.InvariantInfo);
Regards,
You can use the .ToString() method on the DateTime object to format it however you'd like. Your code would look something like this:
DateTime.Now.ToString("M-d-yyyy");
More info on formatting date times can be found on the MSDN: http://msdn.microsoft.com/en-us/library/zdtaw1bw%28v=vs.110%29.aspx
you can try
date.ToString("MM/dd/yy", yyyymmddFormat);
or
try whats in this link
http://social.msdn.microsoft.com/Forums/en-US/af4f5a1e-f81d-47fe-981d-818e785b8847/convert-string-to-datetime-object
you can force the string into a standard format if you like
I have problem when im trying parse datetime in format like: "1.00:29:00" 1- days,29-minutes, after invoke DateTime.Parse im getting "String was not recognized as a valid DateTime"
thanks in advance for any suggestion.
That's not a valid native datetime string format - see the remarks section here for more info - but that sounds a lot like you're really talking about a TimeSpan.
You can use ParseExact providing the format to use along with the value to parse.