I have a problem with format a date in C# - c#

My pc time is in American form M/dd/yyyy h:mm tt so if I create a DateTime for example a DateTime.Now , that will be in a same form as my pc, but I need another format yyyy/MM/dd HH:mm:ss , but I cannot change the DateTime.Now to another format , because I only can do this if the result will be a string , but I need a DateTime typed variable.
I tried to fix this like this:
string formattedDate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
var time = DateTime.ParseExact(formattedDate, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(formattedDate);
Console.WriteLine(time);
but I got this output : Output

A culture can be specified for a thread.
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("en-US");
Console.WriteLine(DateTime.Now); // 1/13/2023 5:36:22 PM
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
Console.WriteLine(DateTime.Now); // 01/13/2023 17:36:22

Related

Convert dd/MM/yyyy HH:mm tt to MM/dd/yyyy HH:mm tt in C#

I want to convert string as : "25/12/2017 4:00 PM" to "12/25/2017 4:00 PM". My code :
var TDXRSC = "25/12/2017 4:00 PM";
DateTime.ParseExact(TDXRSC, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
But it's not working.
The issue is your date format expected is dd/MM/yyyy hh:mm tt but the reference date only has a single digit hour 4. You are probably better off not expect leading zeros for days, months or hours.
Try..
var TDXRSC = "25/12/2017 4:00 PM";
var input = DateTime.ParseExact(TDXRSC, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);
This will also still parse 2 digit hours. So var TDXRSC = "25/12/2017 12:00 PM"; will still parse correctly.
var TDXRSC = "25/12/2017 4:00 PM";
var input = DateTime.ParseExact(TDXRSC, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);
var output = input.ToString("MM/dd/yyyy h:mm tt");
When you call ParseExact you're telling the compiler what format the incoming date is. You can then use ToString() method to provide a format for a string representation of the parsed date.
Hope that .TryParseExtract will be more safe to use for conversion, use like the following:
var dateString = "25/12/2017 4:00 PM";
DateTime inputDate;
if(DateTime.TryParseExact(dateString, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out inputDate))
{
var output = inputDate.ToString("MM/dd/yyyy hh:mm tt");
Console.WriteLine(output);
}
else
{
Console.WriteLine("Conversion failed");
}
Working Example
var TDXRSC = "25/12/2017 4:00 PM";
DateTime date = Convert.ToDateTime(TDXRSC);
string Format = date.ToString("MM/dd/yyyy h:mm tt");

How get DateTime.Now with "AM" not "ق.ظ"

I try to get DateTime.Now , but what I get is:
'23/05/2016 03:16:51 ق.ظ'
I want to have
'23/05/2016 03:16:51 AM'
or something like this
Then you current culture seems to be an arabic one. You can use the overload of DateTime.ToString:
var enUsCulture = new CultureInfo("en-US");
string result = DateTime.Now.ToString( enUsCulture );
an an alternative you could pass the exact format with CultureInfo.InvariantCulture(to avoid that / will be replaced with your current culture's date separator):
string result = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
The InvariantCulture avoids that the "/" custom format specifier will cause / to be replaced with your current culture's date separator.
You could create a specific CultureInfo with your requirements starting from the CurrentCulture
CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.LCID);
ci.DateTimeFormat.AMDesignator = "AM";
ci.DateTimeFormat.PMDesignator = "PM";
ci.DateTimeFormat.LongTimePattern = "HH:mm:ss tt";
string result = DateTime.Now.ToString(ci);
Console.WriteLine(result);
I think CultureInfo.InvariantCulture is the key here
DateTime now = DateTime.Now;
string result = now.ToString("dd MMM yyyy hh:mm tt", CultureInfo.InvariantCulture);
Console.WriteLine(result);
This should give you "23 May 2016 02:32 PM", you can further edit the date and time by using other setting in the string format specifier. But keep InvarianCulture and tt in order to recieve AP/PM.
For all possible formats and some examples check these links
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
http://www.dotnetperls.com/datetime-format

String was not recognized as a valid DateTime. Throws an Exception

I'm trying to convert current date to a specified format.
DateTime date = DateTime.ParseExact(DateTime.Now.ToString(), "yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
I'm receiving the following exception.
String was not recognized as a valid DateTime.
My local TimeZone is (UTC+10:00)Melbourne.
What am I doing wrong here?
Your code (even if it worked), would do nothing. It would simply serialize and deserialize the date. I believe you're looking for this:
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
It doesn't work because DateTime.Now.ToString() is giving a string like (I happen to be in the same timezone, and presumably have the same culture as you):
14/01/2016 3:54:01 PM
Which is of the format:
dd/MM/yyyy h:mm:ss tt
Which does not match the format you're using: yyyy-MM-dd HH:mm:ss.fff
Try this:
string fm = "yyyy-MM-dd HH:mm:ss.fff";
string str = DateTime.Now.ToString(fm, CultureInfo.InvariantCulture);
DateTime dt = DateTime.ParseExact(str, fm, CultureInfo.InvariantCulture);
EDIT:
A better way to achieve the date in the format would be like
DateTime now = DateTime.Now;
CultureInfo culture = new CultureInfo("en-AU"); //Melbourne
Thread.CurrentThread.CurrentCulture = culture;
Console.WriteLine(now.ToString("yyyy-MM-ddTHH:mm:ss.fff"));
IDEONE DEMO

How to find exact format of a date string?

I need to know exact date format that will perse the string 16-Aug-78 12:00:00 AM. I have tried various string like "ss-MMM-yy hh:mm:ss". Is there any way for finding it to converting to any general format. I am using CultureInfo.InvariantCulture class.
Your string format is wrong. It has to match your string format exactly. You can use dd-MMM-yy hh:mm:ss tt format instead.
Here an example in LINQPad.
string s = "16-Aug-78 12:00:00 AM";
var date = DateTime.ParseExact(s, "dd-MMM-yy hh:mm:ss tt",
CultureInfo.InvariantCulture);
date.Dump();
Custom Date and Time Format Strings
Or, since dd-MMM-yy hh:mm:ss tt is a standart date and time format for InvariantCulture, you can directly DateTime.Parse method like;
string s = "16-Aug-78 12:00:00 AM";
var date = DateTime.Parse(s, CultureInfo.InvariantCulture);
date.Dump();
Here a demonstration.
Is there any way for finding it to converting to any general format?
There is no way to get format of a string except you create your own formatting. Only you can know what is your string format exactly, computer can't.
For example; 01/02/2014 can be 1 February 2014 or 2 January 2014 depends on which custom format you can parse it.
Try as below
var dstring = "16-Aug-78 12:00:00 AM";
DateTime result;
var matchingCulture = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(ci => DateTime.TryParse(dstring, ci, DateTimeStyles.None, out result))
You have wrong format string, you are using ss for day it should be dd. This article Custom Date and Time Format Strings explains what you need for custom format.
Use
"dd-MMM-yy hh:mm:ss tt"
Instead of
"ss-MMM-yy hh:mm:ss"
You can use DateTime.ParseExact to parse the string.
DateTime dt = DateTime.ParseExact("16-Aug-78 12:00:00 AM", "dd-MMM-yy hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
You can try with DateTime.TryParseExact():
string strDate = "16-Aug-78 12:00:00 AM";
DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] {"dd-MMM-yy hh:mm:ss tt" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datDate))
{
Console.WriteLine(datDate);
}
else
{
Console.WriteLine("Error in datetime format");
}

DateTime Format for .NET

By using the following code, how do I get the date format to be mm/dd/yyyy HH:MM:SS AM|PM?
string timestamp = DateTime.Now.ToString();
var res = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Read Standard Date and Time Format Strings On MSDN for more information
Below is another way of achieving same format
Console.WriteLine(date1.ToString("G",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30:00 AM

Categories

Resources