I have below code :-
1) String str = DateTime.Now.ToString("M/d/yyyy")
Output :
str gives "2/3/2014"
2) DateTime dt = Convert.ToDateTime(DateTime.Now.ToString("M/d/yyyy"));
Output :
dt.ToString() gives "2014/02/03 12:00:00 "
But I'm not able to understand why it is not giving "2014/2/3 12:00:00 " ,i.e, without leading zeroes in day and month?
DateTime values have no format, it is the DateTime.ToString() method that outputs your datetime value in a particular format.
If you don't specify any parameter in the ToString() then the output is formatted using the general date and time format specifier ('G'). (Usually is the one set up in you control panel international settings)
Console.WriteLine(dt.ToString("M/d/yyyy"));
The default format string for System.DateTime is "G" as in System.DateTime.ToString("G") where G is one of the presets. from source
Edit 1:
Globalization.CultureInfo customCulture = new Globalization.CultureInfo("en-US");
customCulture.DateTimeFormat.ShortDatePattern = "yyyy/M/d";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture;
DateTime dt = Convert.ToDateTime(DateTime.Now.ToString("M/d/yyyy"));
Console.WriteLine(dt.ToString())
When using ToString() without any format, it simply uses the default ToString() method.
Use the format you specified again to get the wanted result.
you can use the Format function for displaying the way you want
string.Format("{0:yyyy/M/d}",dateValue);
regards
Related
I have two parameters one for date and another for time, and i need date value part and time values part.
My two parameters are below.
// For Date parameter
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
bo.Dateused5 = dt;
// For Time parameter
string Fromtiming = ddl_FromHours.SelectedItem.ToString() + ":" + ddl_FromMinutes.SelectedItem.ToString();
DateTime InterviewTime = Convert.ToDateTime(Fromtiming);//StartTime
bo.Dateused4 = InterviewTime;//InterviewTime
so i need to send mail to the candidate to only date part, should not contain time and time part, should not contain date.
are you looking for this:
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
string mailDate = dt.ToString("dd-MMM-yyyy");// will give 01-jan-1999
string date = dt.ToString("dd-MM-yyyy"); // will give 01-01-1999
You can also try using String.Format()
string mailDate = String.Format("{0:dd-MM-yyyy}", dt); // will give 01-01-1999
You can use ToShortDateString():
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
var date = dt.ToShortDateString();
Note that it uses date format attached to the current thread's culture info.
You would need to use strings rather than dates, so change the type of your variables to string so that
bo.Dateused5 = dt.ToString("dd-MMM-yyyy")
would set Dateused5 to a string of the date component, then
bo.Dateused4 = InterviewTime.ToString("HH:MM");
would set Dateused4 to the time component.
Couldn't test your code but I am very sure there are Functions "DateValue" and "TimeValue" you can make use of.
Something like,
Format(DateValue(any datetime), "dd-MM-yyyy")
gives you Only Date in the specified format. Similar way for TimeValue
My code throws an error:
When converting string to datetime, parse the string to take the date before putting each variable into datetime object.
CODE:
I need to display default value in text box i.e. current date.
txtIssuingDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
Then i need to save that in DB too without in specified format
ComposedLetterBizz CompLetterBizz = new ComposedLetterBizz(Convert.ToInt32(txtName.Text),
Convert.ToInt32(HiddenFieldComplaintID.Value),
txtLetterNo.Text,
txtDispatchNo.Text,
txtSubject.Text,
Convert.ToInt16(ddlDepartments.SelectedValue),
Convert.ToInt16(ddlDesignations.SelectedValue),
Convert.ToDateTime(txtIssuingDate.Text),
Convert.ToDateTime(txtDeadLine.Text));
Use DateTime.Parse with CultureInfo.InvariantCulture in case that your culture doesn't use / as date-separator:
So instead of
Convert.ToDateTime(txtIssuingDate.Text)
which uses your current-culture's date-separator, this
DateTime.Parse(txtIssuingDate.Text, CultureInfo.InvariantCulture)
But then you should also do it when you assign the date to the TextBox.Text:
txtIssuingDate.Text = DateTime.Now.ToString(CultureInfo.InvariantCulture);
The "/" Custom Format Specifier
try this
DateTime obj = DateTime.Now;
txtIssuingDate.Text = obj.ToString("dd/MM/yyyy");
ComposedLetterBizz CompLetterBizz = new ComposedLetterBizz(Convert.ToInt32(txtName.Text), Convert.ToInt32(HiddenFieldComplaintID.Value), txtLetterNo.Text, txtDispatchNo.Text, txtSubject.Text, Convert.ToInt16(ddlDepartments.SelectedValue), Convert.ToInt16(ddlDesignations.SelectedValue),
obj, Convert.ToDateTime(txtDeadLine.Text));
string strHijdt ="29-02-1435";
DateTime hdt = DateTime.ParseExact(strHijdt, "dd/MMM/yyyy HH:MI24",
CultureInfo.InvariantCulture);
Getting error while convert to string("29-02-1435") to datetime
2/1435 has 28 days only
so, below will work
string aa="28-02-1435";
DateTime hdt = DateTime.ParseExact(aa, "dd-MM-yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(hdt.ToLongDateString());
DEMO
since you have given input as 29-02-1435 even you provide correct date time format (dd-MM-yyyy) you will get error for the invalid date
Two problems here:
1. As mentioned above, expected format for does not match string (there is no time, different separator)
2. If your date string is in Hijri calendar, you should either provide correct culture explicitly or use system culture (pass null for IFormatProvider):
string strHijdt = "29-02-1435";
var culture = CultureInfo.GetCultureInfo("ar-SA");
DateTime hdt = DateTime.ParseExact(strHijdt, "dd-MM-yyyy", culture);
I am trying to convert my string formated value to date type with format dd/MM/yyyy. It runs fine but when I enter fromdate(dd/MM/yyyy) in textbox its fine and todate(dd/MM/yyyy) in textbox then it gives an error that string was not recognized as a valid datetime.What is the problem exactly i dont know. same code run on another appliction its run fine but in my application it shows Error.
Below I have used array for required format and split also used.
string fromdate = punchin.ToString();
string[] arrfromdate = fromdate.Split('/');
fromdate = arrfromdate[1].ToString() + "/" + arrfromdate[0].ToString() + "/" + arrfromdate[2].ToString();
DateTime d1 = DateTime.Parse(fromdate.ToString());
try with DateTime.TryParseExact as below
DateTime date;
if (DateTime.TryParseExact(inputText, "MM/dd/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out date))
{
// Success
}
if you know the format of input date time you don't need to do any string manipulation.
But you need to give correct Date and Time Format String
I got 5/13/2013 12:21:35 PM in string fromdate
Use DateTime.TryParseExact, You don't have to split your string based on / and then get first three items from the array instead you can simply do:
DateTime dt;
if (DateTime.TryParseExact("5/13/2013 12:21:35 PM",
"M/d/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
//date is fine
}
Using single d and single M as it can accomodate single digit as well as double digits day/Month part. You can simply pass punchin as the string parameter, Calling ToString on string types is redundant.
Try :
DateTime.ParseExact(fromdate, "MM/dd/yy", CultureInfo.InvariantCulture)
Obviously you can reformat the above, and use different providers by creating an instance of CultureInfo related to the string you are parsing, and you can modify the format string to reflect that culture or to accommodate more date parts
I have a date string in format "08/1999" I want to get the first date of the corresponding month. eg : in this case 08/01/1999.
It is simple for en-Us culture. I break the string, append "01" in the string to get 08/01/1999 and then DateTime.Parse(datestring) but this is valid for en-US culture only.
How can I do this for different culture ?
My datestring will always be in mm/yyyy format. and I am trying to obtain a DataTime obj from this dateString.
Use ParseExact method. Note upper-cased M's are for months and lower-cased m's for minutes.
string dateToConvert = "08/1999";
string format = "MM/yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime result = DateTime.ParseExact(dateToConvert, format, provider);
Output:
{1999-08-01 00:00:00}
You can also use Convert.ToDateTime and Parse methods. It will produce the same result, but in implicite way:
DateTime result = Convert.ToDateTime(dateToConvert, provider); // Output: {1999-08-01 00:00:00}
DateTime result = DateTime.Parse(dateToConvert, provider); // Output: {1999-08-01 00:00:00}
Read more at:
Parsing Date and Time Strings
Standard Date and Time Format Strings
Custom Date and Time Format Strings
I'm not sure if I understand your question correctly, but you can try passing CultureInfo.InvariantCulture if you want to force the US date format regardless of the regional settings of the client computer:
DateTime.Parse("08/1999", System.Globalization.CultureInfo.InvariantCulture)
I break the string, append "01" in the string to get 08/01/1999 and then DateTime.Parse(datestring)
That's a very long-winded way to do it. Simply this will work:
DateTime.Parse("08/1999")
How can I do this for different culture ?
If your string is always in this format, do this:
DateTime.Parse("08/1999", CultureInfo.InvariantCulture)