I don't understand why I am finding this such a problem.
I have a simple form with a DateTimePicker on it (called dtpStartDate). I want to have two DateTime objects: one for UK format and one with US format. Something like:
string str = "31/01/2018 10:00:00"; //dtpStartDate.Value.Date.ToString()
DateTime dtUK = DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss", null);
DateTime dtUS = Convert.ToDateTime(dtUK, new CultureInfo("en-US"));
What am I doing wrong? Nothing I have tried has given me what I want, which is two DateTime objects, not a string containing the date and time.
Many thanks
Think of a DateTime object as:
public class DateTime
{
public int Year;
public int Month;
public int Day;
public int Hours;
public int Minutes;
public int Seconds;
public int Milliseconds;
}
That's all it stores, it doesn't contain any Culture-related information. If you were to create two DateTime objects "for UK Format and US Format", they would be identical and contain the same data.
However, when you call the ToString() method on a DateTime object, you can pass it a CultureInfo parameter. That parameter tells the DateTime object how to output those values into the resultant string.
So for a UK CultureInfo with ToString(new CultureInfo("en-GB"), it outputs a string containing dd/MM/yyyy HH:mm:ss.
And for a US CultureInfo with ToString(new CultureInfo("en-US"), it outputs a string containing MM/dd/yyyy HH:mm:ss.
Hope this helps
DateTime contains general datetime information. You can make a culture specific string representation. Try this:
string str = "31/01/2018 10:00:00";
DateTime dt = DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss", null);
var uk = dt.ToString(new CultureInfo("en-GB")); //31/01/2018 10:00:00
var us = dt.ToString(new CultureInfo("en-US")); //1/31/2018 10:00:00 AM
string str = "31/01/2018 10:00:00"; //dtpStartDate.Value.Date.ToString()
DateTime dateTimeUK = DateTime.Parse(str, new CultureInfo("en-GB"));
DateTime dateTimeUS = DateTime.Parse(str, new CultureInfo("en-US"));
Related
I have a string like this:
250920111414
I want to create a DateTime object from that string. As of now, I use substring and do it like this:
string date = 250920111414;
int year = Convert.ToInt32(date.Substring(4, 4));
int month = Convert.ToInt32(date.Substring(2, 2));
...
DateTime dt = new DateTime(year, month, day ...);
Is it possible to use string format, to do the same, without substring?
Absolutely. Guessing the format from your string, you can use ParseExact
string format = "ddMMyyyyHHmm";
DateTime dt = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
or TryParseExact:
DateTime dt;
bool success = DateTime.TryParseExact(value, format,
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
The latter call will simply return false on parse failure, instead of throwing an exception - if you may have bad data which shouldn't cause the overall task to fail (e.g. it's user input, and you just want to prompt them) then this is a better call to use.
EDIT: For more details about the format string details, see "Custom Date and Time Format Strings" in MSDN.
You could use:
DateTime dt = DateTime.ParseExact(
date,
"ddMMyyyyHHmm",
CultureInfo.InvariantCulture);
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);
DateTime Formats
How do you convert a string such as 13.11.2017 into a DateTime with that format 13/11/2017
I tried several ways but i didn't get solution .
string Arrival = Request.QueryString["Arrival"];//13.11.2017
DateTime datee = DateTime.ParseExact(Arrival , "dd/MM/yyyy", null);
here is updated code i split the string :
string Arrival = Request.QueryString["Arrival"];//13.11.2017
string year = Arrival.Split('.')[2];
string month = Arrival.Split('.')[1];
string day = Arrival.Split('.')[0];
string date = day + "/" + month + "/" + year;
DateTime datee = DateTime.ParseExact(date, "dd/MM/yyyy", null);
Observe:
// the input string
string inputString = "13.11.2017";
// parse the input string with a specific format to create a DateTime
DateTime dt = DateTime.ParseExact(inputString, "dd.MM.yyyy", CultureInfo.InvariantCulture);
// create a string from the DateTime using a different specific format
string outputString = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
// output the string
Console.WriteLine(outputString); // "13/11/2017"
Note that a DateTime itself does not have any particular format. A format only comes into play when going to or from a string.
Also note that by using the invariant culture here, we are avoiding any issues that may arise if the current culture was one that used different date separators or a different calendar system.
using System.Text.RegularExpressions;
string Arrival = Request.QueryString["Arrival"]; // 13.11.2017
string dateString = Regex.Replace(Arrival, #"\.", "/"); // 13/11/2017
DateTime date = DateTime.ParseExact(dateString , "dd/MM/yyyy", null); // 11/13/2017 00:00:00
Had the same problem with Russian locale.
Currently using ISO format everywhere to avoid incorrect DateTime understandings (like MM.dd when you mean dd.MM).
public static string ToIsoStr(this DateTime dateTime)
{
return dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
}
Your task will be solved with
return dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
I am trying to convert a string, 20151107 to the date format of 2015-11-07.
Here's my code :
public static DateTime CustomDateFormat(this string resultdate)
{
DateTime dt = DateTime.ParseExact(resultdate, "yyyyMMdd", CultureInfo.InvariantCulture);
return dt;
}
However this returns something like this 11/07/2015 12:00:00 AM.
Any idea?
Your date returns like that because you are returning the entire DateTime object and since you are not providing a time it is default to 00:00:00.00.
If you want to return the Date in a particular format, you can use the Standard Format Strings or a Custom Format String.
In your case, you want 2015-11-07 which is a custom format of yyyy-MM-dd and can be used like so:
public static string CustomDateFormat(string resultdate)
{
DateTime dt = DateTime.ParseExact(resultdate, "yyyyMMdd", CultureInfo.InvariantCulture);
return dt.ToString("yyyy-MM-dd");
}
public static String CustomDateFormat(this string resultdate)
{
DateTime dt = DateTime.ParseExact(resultdate, "yyyyMMdd", CultureInfo.InvariantCulture);
return dt.ToString("yyyy-MMM-dd");
}
string DateCreated = "2012-05-24 12:34:40.060"; USA culture
How do I create the string into a Date time object with a format that include the month,
day, year, hour, minutes, seconds, milliseconds.
I think the format is this "MM/dd/yyyy hh:mm:ss.fff tt" but not sure.
** ** = underline in red, its an error
I need the datetime object because I want to pass it through another function that only accept a value of "DateTime" and not a string
Any help would be appreciated.
Thank You
this is what I have so far but i know its wrong
string DateCreated = "2012-05-24 12:34:40.060";
DateTime dt = **new DateTime(DateCreated);**
DateTime dateCreated = **dateCreated.ToString("MM/dd/yyyy hh:mm:ss.fff tt")**;
var dateTime = DateTime.ParseExact(DateCreated, "yyyy-MM-dd HH:mm:ss.fff");
string dateCreated = "2012-05-24 12:34:40.060";
DateTime myDate = DateTime.ParseExact(dateCreated, "yyyy-MM-dd HH:mm:ss.fff",
System.Globalization.CultureInfo.InvariantCulture)
you can also use
Convert.ToDateTime Method (String)
I would like to have my end result in date format as per the specified format i.e YYMMDD how can i get this from a string given as below
string s="110326";
From string to date:
DateTime d = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
Or the other way around:
string s = d.ToString("yyMMdd");
Also see this article: Custom Date and Time Format Strings
DateTime dateTime = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
although id recommend
DateTime dateTime;
if (DateTime.TryParseExact(s, "yyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime))
{
// Process
}
else
{
// Handle Invalid Date
}
To convert DateTime to some format, you could do,
string str = DateTime.Now.ToString("yyMMdd");
To convert string Date in some format to DateTime object, you could do
DateTime dt = DateTime.ParseExact(str, "yyMMdd", null); //Let str="110719"