I have a double value in seconds and i would like to use a Numeric Format String to display it as mm:ss or hh:mm:ss.
Is this possible? Havent found anything about it on MSDN?
The reason is that we use a Telerik Chart that displays our data and since I cant change the uderlying format I have to hook the format string into their chart api.
Thanks in advance
Johan
You're looking for the TimeSpan class.
Use the following:
var ts = new TimeSpan(0, 0, 0, (int)doubleValueOfSeconds, 0);
Of course you might want to consider more accurate rounding of the double value, rather than truncation as in my example.
Take a look at TimeSpan on MSDN to get the string value, though the simplest current culture-specific thing would be TimeSpan.ToString().
UPDATE to updated question:
You can't directly format a number of seconds to a date and time. Formatting is essentially string manipulation, and you need it to do some maths before that formatting happens.
Use my example above, then call TimeSpan.ToString(formatString); to get the string values to give to the control.
That's assuming you haven't simply missed something. I personally think the Telerik controls are waaaay to complex, but I'm sure they'll have formatting built-in. Essentially, write code using my example above to create the data with timespans, then use the inbuilt format string to tell the control how to format the timespan for the given data series.
Related
I have ZonedDateTime value. I need to obtain corresponding UTC time and to format it as ISO8601 string (without Time Zone).
What's the right "NodaTime way" of doing it?
I understand that I can use ZonedDateTime.ToDateTimeUtc() method to get .Net DateTime Kind of Utc. Should I do it and then just use ToString()?
Like
var myresult = resZonedDateTime.ToDateTimeUtc().ToString("s")
Should I really use "s" there?
There are a few different ways, depending on exactly what you're looking for.
First, you need to decide whether you want a value like 2014-10-30T16:46:49 or one like 2014-10-30T16:46:49Z. Both are allowed by ISO8601, but the trailing Z at the end is specifically used when the value is UTC. If you send the string elsewhere, the receiving party will not have to guess what the basis for the value is.
If you want the Z, then convert your ZonedDateTime to an Instant using the .ToInstant() method.
Instant instant = zdt.ToInstant();
If you don't want the Z, but still want the value to reflect UTC, then adjust your ZonedDateTime to UTC, then strip it down to the LocalDateTime using it's .LocalDateTime property.
LocalDateTime ldt = zdt.WithZone(DateTimeZone.Utc).LocalDateTime;
Next, you need to decide whether you want to format the string inline with the BCL-based API, or whether you want to use the pattern-based API. Noda Time supports both, which you can read about in the user guide.
The BCL-based API allows you to use methods you're already familiar with, such as .ToString() and .ToString("some format", someCulture) For example:
string s = instant.ToString();
or
string s = ldt.ToString("s", CultureInfo.InvariantCulture);
The pattern-based API separates the work of parsing the format string from doing the actual formatting, as two steps:
var pattern = InstantPattern.ExtendedIsoPattern;
var s = pattern.Format(instant);
It is much more efficient if you are going to be formatting many items. I typically use it when I'm working with LINQ. For example:
var pattern = InstantPattern.GeneralPattern;
var strings = instants.Select(pattern.Format);
or
var pattern = LocalDateTimePattern.GeneralIsoPattern;
var strings = ldts.Select(pattern.Format);
And finally, you need to think about the precision you want. In the pattern API, the "general" formats are precise to whole seconds. The "extended" formats include fractional seconds up to 7 decimal places. You could also create your own pattern using the Create... static methods on the various pattern classes.
If you are just using ToString, then keep in mind that the default format may or may not be what you are looking for. Again, you can pass a format string. For Instant values, "g" is precise to whole seconds, but you would use a custom string of "yyyy'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFF'Z'" if you need precision. For LocalDateTime values, "s" is precise to whole seconds, and "o" is precise to 7 decimals.
Again, all of this is in the "Text" chapter of the user guide, which I would encourage you to read.
I'm creating simple table report with column of TimeSpan type.
I'm summing its values, which leads into values bigger than 24 hours, into Text componnent.
{SumTime(DataBand1,Records.time)}
I'm trying to format Text field like HH:mm:ss, but for 25 hours it gives me 01:00:00 (or 1.01:00:00 with general formatting) instead of 25:00:00 what is my goal.
Edit: The problem is not how to format timespan, but how to associate formatted value into Stimulsoft's Text component.
The Hours property has a maximum of 24h. You could format it yourself using String.Format and TimeSpan.TotalHours:
string text = string.Format("{0}:{1}:{2}",
(int) Records.time.TotalHours, // TotalHours is double
Records.time.Minutes,
Records.time.Seconds);
I know this is kind of an old question, but I stumbled upon it looking for a solution myself. Having done some digging, here's what I've come up with:
Assuming you already have a timespan variable (either setting it in the datasource, or having a variable set with DateDiff), you can format it with the following:
{string.Format("{0}:{1}:{2}",
(int) Variable1.TotalHours,
Variable1.Minutes,
Variable1.Seconds)}
Lets say you have two different fields (named Date1 and Date2) that you need to get the difference of, and don't feel like putting it into a variable:
{string.Format("{0}:{1}:{2}",
(int) DateDiff(Date2,Date1).TotalHours,
DateDiff(Date2,Date1).Minutes,
DateDiff(Date2,Date1).Seconds)}
I have a string date like 'Wednesday, May 15, 2013' when I Parse it o lost the original format, is there a way to know what was the original format date or get it before the final Parse?
No, you'll lose the original format, if will now be a different object type with no care for its original format prior to parsing.
Your best option would be to store the pre-parse format prior to parsing as a different variable.
However if you simply wish to format the date in that original format, see Farhad's answer.
You can use this for getting date in a fromat you wish.
String.Format("{0:D}", DateTime.Now); // Tuseday, May 21, 2013
The DateTime struct does not have a format, it stores all of those values on various properties. When you want to display it you specify which format to use.
Using a format specifier when displaying it will likely do what you want. This mdsn article provides some basic information on format specifiers. The only way you can use the exact format you show there is if you know it ahead of time and have a format specifier for it. If you know you'll want to display strings in that format throughout the program it will be easy, if you get many different formats and want to decide how to display your dt at runtime it will be fairly complicated. I'm sure you could write some code to figure out what it is, but once the DateTime is created it will have no notion of what format the string used to create it was in.
In My Asp.net webpage I need to display today's date into one of the textbox , so in my form load I wrote the following code
textbox1.text = System.DateTime.Today.ToShortDateString();
this line is giving me date like 1/7/09 but I want date like 01/07/09 , Is there anyway I can conver this date into mm/dd/yy format in C#?
DateTime.Today.ToString("MM/dd/yy")
Look at the docs for custom date and time format strings for more info.
(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)
DateTime.Today.ToString("MM/dd/yy")
Look at the docs for custom date and time format strings for more info.
(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)
Others cultures really are a problem. For example, that code in portugues returns someting like 01-01-01 instead of 01/01/01. I also don't undestand why...
To resolve that problem i do someting like this:
IFormatProvider yyyymmddFormat = new System.Globalization.CultureInfo(String.Empty, false);
return date.ToString("MM/dd/yy", yyyymmddFormat);
Have you tried the following?:
textbox1.text = System.DateTime.Today.ToString("MM/dd/yy");
Be aware that 2 digit years could be bad in the future...
Look into using the ToString() method with a specified format.
See, here you can get only date by passing a format string.
You can get a different date format as per your requirement as given below for current date:
DateTime.Now.ToString("M/d/yyyy");
Result : "9/1/2016"
DateTime.Now.ToString("M-d-yyyy");
Result : "9-1-2016"
DateTime.Now.ToString("yyyy-MM-dd");
Result : "2016-09-01"
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Result : "2016-09-01 09:20:10"
For more details take a look at MSDN reference for Custom Date and Time Format Strings
I have a datetime coming back from an XML file in the format:
20080916 11:02
as in
yyyymm hh:ss
How can I get the datetime.parse function to pick up on this? Ie parse it without erroring?
DateTime.ParseExact(input,"yyyyMMdd HH:mm",null);
assuming you meant to say that minutes followed the hours, not seconds - your example is a little confusing.
The ParseExact documentation details other overloads, in case you want to have the parse automatically convert to Universal Time or something like that.
As #Joel Coehoorn mentions, there's also the option of using TryParseExact, which will return a Boolean value indicating success or failure of the operation - I'm still on .Net 1.1, so I often forget this one.
If you need to parse other formats, you can check out the Standard DateTime Format Strings.
Thanks for the tip, i used this to get my date "20071122" parsed, I needed to add datetimestyles, I used none and it worked:
DateTime dt = DateTime.MinValue;
DateTime.TryParseExact("20071122", "yyyyMMdd", null,System.Globalization.DateTimeStyles.None, out dt);