I have variable of a type DateTime objApplicationSummaryInfo.AdmissionDate. I am trying to assign a value, such as
objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
TextBox3.Text.ToString(), "dd/mm/yyyy", null);
But when i am assigning a value like 27/09/2012to a textbox3, the variable objApplicationSummaryInfo.AdmissionDate takes a value 1/27/2012 12:00:09. The format as well as the date comes back incorrect.
What possible code am I missing and what can be an alternate solution. Thanks for assistance.
mm is minutes.
MM is months
Custom Date and Time Format Strings
Your code should be:
objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
TextBox3.Text.ToString(), "dd/MM/yyyy", null);
You will also need to set the culture:
CultureInfo provider = CultureInfo.InvariantCulture;
objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
TextBox3.Text.ToString(), "dd/MM/yyyy", culture);
Otherwise it will use the culture of the machine running the code.
"dd/mm/yyyy" should probably be "dd/MM/yyyy"
mm - is for minutes
MM - is for months
objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
TextBox3.Text.ToString(), "dd/MM/yyyy", null);
You can try with - dd/MM/yyyy
DateTime.ParseExact(TextBox3.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
hey are you Testing on localHost try it on Server i will work on server but not on localhots
Related
I have a date field and the format is "dd-M-y", example 01-Jan-2013. First I want to check the format which must be "dd-M-y" and secondly the date shouldn't be in the past but can be today and onward.
How would I do that? I would like to use regular expressions but I don't know what a suitable one would be.
You should use DateTime.TryParseExact rather than using Regex to validate your DateTime
string testDate = "01-Jan-2013";
DateTime temp;
if (DateTime.TryParseExact(testDate,
"dd-MMM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out temp) &&
(temp > DateTime.Today))
{
//Valid date and greater than today
}
else
{
//Invalid date or less than today
}
I think you should bind the user to fill the date in correct format instead of checking for it...
The Best solution in this case would be MaskEditExtender
objTour.tourStartDate =
Convert.ToDateTime(
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy", null)
.ToString("MM/dd/yyyy"));
where txtTourStartDate.Text="16/08/2012".
I have searched and read all posts related to this.
In a custom date format string, / denotes the culture-specific date separator, not the literal character /. Thus, the result of your code depends on the user's (or the server's) localization settings.
To make your code independent of culture-specific settings, you have two options:
Explicitly specify a culture that uses a slash as the date separator, e.g.
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy",
CultureInfo.InvariantCulture)
or escape the character, e.g.
DateTime.ParseExact(txtTourStartDate.Text, #"dd\/MM\/yyyy", null)
(note the # and the \).
Both should yield the desired result.
This will be enough:
objTour.tourStartDate = DateTime.ParseExact(txtTourStartDate.Text,
"dd/MM/yyyy",
CultureInfo.InvariantCulture);
Your original code works, although you are doing lot of unnecessary conversions. (DateTime -> ToString -> ToDateTime), the real issue is InvariantCulture. Since you are passing null for CultureInfo try CultureInfo.InvariantCulture.
Your original code:
objTour.tourStartDate =
Convert.ToDateTime(
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy"));
A better one could be:
objTour.tourStartDate =
DateTime.ParseExact(txtTourStartDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
I'm parsing an XML file from an external source, and I have 2 attributes which contain the date and time respectively. I'm looking for the best way to get these into a format I can parse as a date so I can do things with it, but at the moment I'm just getting errors or no results with the methods I've tried.
The date is in the format "20111215" - which is yyyymmdd as it's UK based.
The time is formatted as "1417+0000" which I presume is the time plus offset from GMT?
Basically I need to get these into UK time. I've tried using DateTime.Parse on the separate parts but both give an error as not valid format. Tried String.Format on the date part but that didn't change it at all. I presume I need to combine the 2 before parsing but I'm not sure if I need to do anything else with it to make it acceptable.
Any help appreciated.
Use a DateTimeOffset to incorporate the timezone into the DateTime.
string date = "20111215";
string time = "1417+0500";
string dateAndTime = date + time;
string format = "yyyyMMddHHmmzzz";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTimeOffset t = DateTimeOffset.ParseExact(dateAndTime, format, provider);
If you concatenate the fields together, you can then use DateTime.TryParseExact in order to parse them into a DateTime.
string input = string.Format("{0} {1}", dateString, timeString);
DateTime parsed;
if(DateTime.TryParseExact(input,
"yyyyMMdd HHmmK",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out parsed))
{
// parsed OK, use the parsed variable
}
string date = "20111215";
string time = "1417+0000";
string dateString = date + time;;
string format = "yyyyMMddHHmmK";
// or something similar, I'm not sure about the timezone
DateTime result = DateTime.ParseExact(dateString,
format,
CultureInfo.InvariantCulture);
I think this should work (i didn't test it):
string dateString = "20111215";
string timeString = "1417+0000";
int year = int.Parse(dateString.Substring(0,4));
int month = int.Parse(dateString.Substring(4,2));
int day = int.Parse(dateString.Substring(6,2));
int hour = int.Parse(dateString.Substring(0,2));
int mins = int.Parse(dateString.Substring(2,2));
DateTime d = new DateTime(year, month, day, hour, mins, 0);
I am facing a problem in date format. I am converting a string date(dd/MM/yyyy) to datetime, using convert.toDateTime(). It works fine on my local machine but causes problem when I run from server. So to set uniformality, I tried to set culture info for (yyyy-MM-dd HH:mm:ss) format, but couldn't set as .net shows an error.
I tried like this.
CultureInfo DateInfo = new CultureInfo("yyyy-MM-dd HH:mm:ss");
How can I set the culture info for this format?
Try to use like this...
string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
string format = "ddd dd MMM h:mm tt yyyy";
DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Response.Write(dateTime);
hope this may helpful...
How can I convert the english date 10/29/2010 or any language date to user culture date format
I am using the following code
CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
cultureInfo.DateTimeFormat.
string lng = cultureInfo.TwoLetterISOLanguageName;
DateTime dateTime = DateTime.Parse("10/29/2010", cultureInfo);
but it throws error when I try to parse it.
Any Idea how can I resolve this issue
Thanx
Use ParseExact with English (or invariant) culture to convert the String into a datetime, then you can use ToString to output in in the user's date format.
// this is in "d" (= short date) format of the invariant culture
var englishDateString = "10/29/2010";
// convert it to a datetime
var date = DateTime.ParseExact(englishDateString, "d", CultureInfo.InvariantCulture);
// now you can output the date in the user's culture
var localizedDateString = date.ToString("d");
If you want to be explicit, you can add CultureInfo.CurrentCulture as a second parameter to ToString, but it's not required, since this is the default if no culture is specified.
Here's an example of parsing a US date:
DateTime.Parse("10/29/2010", new CultureInfo("en-US"));
i had the same pain when facing date conversion and i used a function (code below), you can modify it as you wish. try it or get ideas from it and let me know if it was useful
Imports Microsoft.VisualBasic Imports System.Globalization Public Class DatumKonvert1
Public Shared Function DK1(ByVal myDMstring As String) As Date
Dim source As String = myDMstring
Dim d As DateTime = DateTime.ParseExact(source, "d'/'M'/'yyyy", CultureInfo.InvariantCulture)
Dim resultMydate As String = d.ToString("M'/'d'/'yyyy")
Dim mdx = DateTime.ParseExact(resultMydate, "M'/'d'/'yyyy", CultureInfo.InvariantCulture)
Return mdx End Function End Class
Use this:
public static string ChangeDateToUserFormat(string dateValue, string dateCulture)
{
CultureInfo dateCultureInfo = CultureInfo.GetCultureInfoByIetfLanguageTag(dateCulture);
DateTime date = DateTime.Parse(dateValue, dateCultureInfo);
return date.ToString(CultureInfo.CurrentCulture);
}
For example:
string date = ChangeDateToUserFormat("10\29\2010", "en-US");
Use DateTime.ParseExact("10/29/2010", "MM/dd/yyyy", CultureInfo.InvariantCulture); instead of DateTime.Parse
Once you have a DateTime, it is no longer bound to a specific culture, but can be output however you want it. Usually, this is with one of the DateTime .ToString methods, or shortcuts like .ToShortDateString(), which uses the current thread's culture.
Edit: Note, it helps if I put the month and day ones in the correct spots. Whoops.