Display HH:mm:ss in DateTime x axis in Chart C# - c#

I have series of data (temperature vs time) and I want to plot it using Chart object of .NET 4.0. The problem is that if I choose DateTime as the XValueType for the series it is displayed like dd.mm.yyyy but I want to display it as HH:mm:ss.
How can I do that?
Thanks for your help

Solution 1:
Try This:
DateTime myDateValue = DateTime.Now;
String XValueType = myDateValue.ToString("HH:mm:ss");
Solution 2:
if you are using windows chart control then
Try This:
chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "HH:mm:ss";
Solution 3: as you said in your below comments if you are showing only DateTime as a string it would be difficult while comparing.
Yes it would be difficult if you only show the Time part (HH:mm:ss) as string.
but if you display the Date and Time then you can again convert back the string to DateTime and perform conversion.
Try This:
DateTime date=DateTime.Now;
axisLabel=date.ToString("dd.MM.yyyy HH:mm:ss");
you can convert the datetime string back to DateTime as below:
DateTime date = DateTime.ParseExact(axisLabel,"dd.MM.yyyy
HH:mm:ss",CultureInfo.InvariantCulture);

Related

Export DATE format data from Access DB to ListView in C #

help please, here is my code:
ListViewItem ekle = new ListViewItem();
ekle.Text = oku["ad"].ToString();
ekle.SubItems.Add(oku["soyad"].ToString());
ekle.SubItems.Add(oku["dogum_tarix"].ToString());
listView2.Items.Add(ekle);
With this code I get date format like this DD/mm/yyyy hh:mm:ss in ListView but I don't want the last
format hh:mm:ss to appear on the list. I tried many ways but can't figure out how to solve it. How can I get only DD/mm/yyyy format on list?
Use the answer from this link:
How to convert MS Access date to string in C#
Here is a sample code, assuming that oku is your DataReader:
Method 1:
DateTime dt = oku.GetDateTime("NameOfTheColumn");
ekle.SubItems.Add(dt.ToString(CultureInfo.CurrentCulture, "dd/MM/yyyy"));
Method 2:
// Dont forget to include using System.Globalization;
DateTime dateValue;
dateValue = DateTime.ParseExact(oku["ColumnName"].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
string result = dateValue.ToString("dd/MM/yyyy");
yyyy is the year, MM is the Month, dd is the day.
The / is the separator. You can use spaces, -, or any other characters, as long as it's not on the code list of DateTime (see below link).
You can switch its places, depending on your preferences
Here is a link with a list of strings used to format your DateTime:
C-SharpCorner.com (Date and Time)

Convert date time to string and back to date time

I'm having a troubles with converting strings to DateTime. Here is what I have. First I convert current date to string (this will be folder name).
string dateString = string.Format("{0:yyyy-MM-dd_HH-mm-ss}", DateTime.Now);
Output like this
2013-05-16_09-32-47
Then I create a folder. During program execution I get this folder and I need to convert it's name back to DateTime. Try to make it like this
DateTime directoreDate = DateTime.ParseExact(directory.Name, "0:yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture);
But it throws FormatException. Can anybody tell me why this happening.
You are using the same composite format string that you used to format the original DateTime. This is not needed for ParseExact - drop the 0: from it:
DateTime directoreDate = DateTime.ParseExact(directory.Name,
"yyyy-MM-dd_HH-mm-ss",
CultureInfo.InvariantCulture);
Use
DateTime directoreDate = DateTime.ParseExact(directory.Name, "yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture);
Remove 0: from DateTime.ParseExact, It was used as a place holder in string.Format().
Use as :
DateTime directoreDate = DateTime.ParseExact(directory.Name,
"yyyy-MM-dd_HH-mm-ss",
CultureInfo.InvariantCulture);

Calendar Extender date picking and converting into correct format

i am picking the value from calender extender in textbox and i am getting the value in the format {"MM/dd/yyyy"} but i want it in the format {"dd/MM/yyyy"} in another textbox
( txt_actualrightformat.Text) as code shown below
DateTime wrongformat = DateTime.Parse(TextBox4.Text);
String rightformat = String.Format("{0:dd/MM/yyyy}", wrongformat.Date);
txt_actualrightformat.Text = rightformat.ToString();
DateTime is irespective of the format, format is only for displaying purpose. If you are not getting the right date in wrongformat then you can use DateTime.ParseExact with the format. and then simply
txt_actualrightformat.Text = wrongformat.ToString("dd/MM/yyyy");
EDIT:
use DateTime.ParseExcat like:
DateTime dt = DateTime.ParseExact(TextBox4.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture);
txt_actualrightformat = dt.ToString("dd/MM/yyyy");
try these
DateTime wrongformat = DateTime.Parse(TextBox4.Text);
txt_actualrightformat.Text =wrongformat.ToString("dd'/'MM'/'yyyy");
or
txt_actualrightformat.Text =String.Format(CultureInfo.InvariantCulture, "{0:dd/MM/yyyy}", _wrongformat )
update:
i think the date in the TextBox4is really in the wrongformat :-)
Note that "22/3/2013" It matches the format "d/M/yyyy" and doesn't match the format "dd/MM/yyyy". - for "dd/MM/yyyy" it should be "22/03/2013".
DateTime dt;
if(DateTime.TryParseExact(TextBox4.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out td))
{
// Valid date used in `TextBox4` (NOTE : dd/MM/yyyy)!, you can use dt now as i explained above!.:-)
}
Before parsing or converting any datetime value check your calendar's date format. on your aspx page go to the calendarExtender and go to its properties then find format and set the format to dd/MM/yyyy and todays date format also do the same. Then from your code behind declare a variable in datetime type and simply pass the text box value which calendarExtender puts the date on and convert the textbox value in date time. The code will be like
public DateTime date{get;set;}
date = Convert.ToDateTime(txtDate.Text.ToString());
In sql server the value will save like "2016-02-09 00:00:00.000" this format

Trouble bringing in a string with d-MMM-yy format parse.exact to date but keeping date format and date type for sorting

As the title said I am bringing in a date string from a datatable with the format of "d-MMM-yy" or 27-AUG-06.
I need to convert it to a date type for sorting, but I need to keep the same format for display.
NOTE: I am using C#, .Net 2.0, and I am retyping this code so bear with me on typos
System.Globalization.DateTimeFormatInfo dtfi;
dtfi = new System.Globalization.DateTimeFormatInfo();
dtfi.ShortDatePattern = "d-MMM-yy";
dtfi.DateSeperator = "-";
//this is in a for loop with rowCnt being the row index/counter: loop and datatable is working fine.
//"newRow" represents a DataRow in the new table.
// the table [row] [column] is bringing in the string date like "27-AUG-06"
//colXDate IS RECORDED AS {8/27/2006 12:00:00 AM}
DateTime colXDate = DateTime.ParseExact(inputDataTable.Rows[rowCnt]["colX"].ToString(), "d-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);
//#### THIS NEXT LINE IS WHERE IT GIVES ME AN ERROR "String was not recognized as valid datetime."
newRow["colX"] = Convert.ToDateTime(colXDate.ToString(), dtfi);
Since you already have colXDate as a DateTime, you don't need to convert it to a string and then back into a datetime.
Instead, try this:
newRow["colX"] = colXDate.ToString("d-MMM-yy");
Your line fails because ToString() will output the date in the format defined in the current culture and you are trying to convert it back to a date using your custom date format.
You need to set it to colXDate.ToString("d-MMM-yy").
SCENARIO: I am passing a datatable back as a DataGrids datasource. I am passing in a string field that is a date. I want to convert it to a date so that I can sort by it but I also want to maintain the same format that it was on the string that came in.
ISSUE: From some of my research it seems that the DateTime type is exactly that a datatype in whatever datatable that you put it in and is not formatable. So even though I bring in an unormal string date and convert it to a datetime via the DateTime.ParseExact, when I put it into my datetime field and try to format it (newRow["colX"] = colXDate.ToString("d-MMM-yy"); //as Scott had above) it still goes in as a set datetime format with hours...etc.
SOLUTION: So I resolved this by smoke and mirrors. I put 2 columns in the datatable, the first being the string format display date and the second the datetime type. On ItemDataBound I hid the datetime column (e.Item.Cells[5].Visible = false;) and then on the sort event I test for string date column (e.SortExpression == "colX") and if it is true I sort by the hidden datetime column.

convert string to date without time

This line
System.DateTime.Parse("09/12/2009");
convert date (string) to 9/12/2009 12:00:00 AM. How can I get a date in the form 9/12/2009.
after explanations I do:
DateTime dt = System.DateTime.Parse(Request.Form["datepicker"]);
dt.ToString("dd/mm/yyyy");
/* and this code have time, why???*/
Your problem is not with the parsing but with the output. Look at how ToString works for DateTime, or use this example:
using System;
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Parse("09/12/2009");
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
}
}
Or to get something in your locale:
Console.WriteLine(dt.ToShortDateString());
Update: Your update to the question implies that you do not understand fully my answer yet, so I'll add a little more explanation. There is no Date in .NET - there is only DateTime. If you want to represent a date in .NET, you do so by storing the time midnight at the start of that day. The time must always be stored, even if you don't need it. You cannot remove it. The important point is that when you display this DateTime to a user, you only show them the Date part.
Anything parsed to a DateTime object will contain date+time. If no time is sepcified, the assumed value will be 0000hr.
To get the date-only representation, it's up to how you format it to string.
E.g. theDate.ToString("dd/MM/yyyy")
Refer to the MSDN Date and Time Format Strings.
if you want to convert gridview datetime column to date only column use this code :
raddgvDaybook.Rows.ToList().ForEach(x =>
{
DateTime dt = DateTime.Parse(x.Cells["Vocdate"].Value.ToString());
string str = string.Format("{0:MM/dd/yyyy}", dt);
x.Cells["Vocdate"].Value = str;
});
I tested the code it will work if you bind the datetime as string in dgv.

Categories