DateTime.ParseExact - different outputs on Windows 10 and WinServer2012R2 [duplicate] - c#

This question already has answers here:
Parsing "12/25/35" to a date using InvariantCulture (2-digit year)
(1 answer)
DateTime.ParseExact - why yy turns into 2015 not 1915
(2 answers)
DateTime.TryParse century control C#
(6 answers)
DateTime TryParse - mapping '99' to 2099, not 1999 [duplicate]
(2 answers)
Closed 1 year ago.
I found out that this line:
DateTime.ParseExact("Jan 30", "MMM yy", null, System.Globalization.DateTimeStyles.None)
creates the date:
2030-01-01
...on my Windows 10 machine, but on Windows Server 2012 R2 the output is:
1930-01-01
Does anybody know how to get Windows Server 2012 to parse the date as 2000 instead of 1900?

It's based on the culture's default calendar's TwoDigitYearMax property. If you change that property value, you'll get a different result:
using System;
using System.Globalization;
class Program
{
static void Main(string[] args)
{
var culture = CultureInfo.CurrentCulture;
// Clone returns a *mutable* copy.
culture = (CultureInfo) culture.Clone();
culture.DateTimeFormat.Calendar.TwoDigitYearMax = 2029;
var result = DateTime.ParseExact("Jan 30", "MMM yy", culture, System.Globalization.DateTimeStyles.None);
Console.WriteLine(result.Year); // 1930
culture.DateTimeFormat.Calendar.TwoDigitYearMax = 2129;
result = DateTime.ParseExact("Jan 30", "MMM yy", culture, System.Globalization.DateTimeStyles.None);
Console.WriteLine(result.Year); // 2030
}
}

Related

How to make formatException gone in Datetime [duplicate]

This question already has answers here:
Convert string to DateTime in c#
(3 answers)
DateTime.Parse throw exception in C#
(1 answer)
DateTime.Parse throwing format exception
(3 answers)
Closed 2 years ago.
Hi I just want to know how to make formatException gone when I'm inserting Datetime.
coding is in down below:
DateTime dob = Convert.ToDateTime(z);
dob.ToString("yyMMdd");
DateTime today = DateTime.Today;
today.ToString("yyMMdd");
var year = today.Year - dob.Year;
age = Convert.ToString(year);
return a;
Try use DateTime.TryParse or DateTime.TryParseExact to check your datetime string and store to your variable after parse at the same time
Use the DateTime.ParseExact() method to convert your date string z to a DateTime value -
var z = "23/09/2020";
DateTime dob = DateTime.ParseExact(z, "dd/MM/yyyy", null);
As you can see, you have to pass the date format you are trying to convert to the second parameter of the ParseExact() method as a string;

Unable to convert String to Date Time [duplicate]

This question already has answers here:
Convert string to DateTime in c#
(3 answers)
Closed 3 years ago.
How do I convert string to date time? I am getting "2019-06-07T02" as string. I want to format string to date like below examples
Ex: 2019-06-07T02 -- Friday January 07,2019 2:00 AM
Ex: 2019-06-07T14 --- Friday January 07,2019 2:00 PM
var input = "2019-06-07T14";
var datetime = DateTime.ParseExact(input, "yyyy-MM-dd'T'HH", CultureInfo.InvariantCulture);
var output = datetime.ToString("dddd MMMM dd, yyyy h':'mm tt");
This does exactly what you need. (Source Docs)
Edit: I know it might be too manual but if you want to configure it more, this way you can, if not, go with oleksa's answer.
var str = "2019-06-07T02";
var dt = DateTime.ParseExact(str, "yyyy-MM-ddThh", CultureInfo.InvariantCulture);
var longstr = dt.ToLongDateString() + dt.ToLongTimeString();
please note that ToLongDateString and ToLongTimeString depends on windows user regional settings
the documentation

Get PM or AM from string date C# [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 4 years ago.
I have this string as a date and I want to add the AM/PM to the end.
From
10-17-2018 00:00:00
To
10-17-2018 00:00:00 AM
How is possible?
Thanks a lot!
So convert the string date into a real DateTime, then ToString() it back out in the format you like:
var dateStr = "10-17-2018 00:00:00";
var date = DateTime.Parse(dateStr);
Console.WriteLine(date.ToString("MM-dd-yyyy HH:mm:ss tt"));
Output: 10-17-2018 00:00:00 AM
DateTime formats: http://www.csharp-examples.net/string-format-datetime/

String format with c# [duplicate]

This question already has answers here:
Converting dd/mm/yyyy formatted string to Datetime [duplicate]
(3 answers)
Closed 7 years ago.
I have this string value
var d = "2016-01-07 09"
It's a string that contains a date with hour without minutes, seconds and millis.
I want to trasform into Datetime with this format
2016-01-07 09:00:00:000
I know that it sounds stupid but this stuff makes me crazy.
Thanks for support.
You have a Date with format Year - Month - Day Hour (yyyy-MM-dd HH).
You can use DateTime ParseExact method . If you know your dates format. You can parse it .
Check dateformats on this link
string d= "2016-01-08 03";
var c = DateTime.ParseExact(d, "yyyy-MM-dd HH", CultureInfo.InvariantCulture);
return c;

Convert string to datetime now format [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 8 years ago.
How I can convert string 23:59 to datetime format?
So that I can reduce the current time in it.
Example:
String = 23:59 minus
Datime.Now = 13.8.2014 10:59:55
Time left 12 hours 0 minutes.
i would use ParseExact
TimeSpan tsDifference = DateTime.ParseExact("23:59", "HH:mm", System.Globalization.CultureInfo.InvariantCulture) - DateTime.Now;
use this :
DateTime date = Convert.ToDateTime(string);
System.TimeSpan diff= DateTime.Now.Subtract(date);
diff will contain your deffernce

Categories

Resources