I am trying to calculate create a time remaining calculator in VB.NET and it won't let me and I can't seem to figure out why. Here is my code
Dim PrefendinedDateTime As DateTime = "3:00:00"
Dim TimeNow As DateTime = DateTime.Now
Dim ElapsedTime As TimeSpan = (TimeNow - frmStartDateTime)
Dim TimeRemaining As TimeSpan = PrefendinedDateTime - New DateTime(ElapsedTime.Ticks)
txtTimeRemaining.Text = New DateTime(TimeRemaining.Ticks).ToString("HH:mm:ss")
I get this error message:
Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
Parameter name: ticks
Not quite sure what this means
You cannot cast a timespan to a date, because those are different ticks. What you need is this:
txtTimeRemaining.Text = TimeRemaining.ToString("g")
or this:
txtTimeRemaining.Text = TimeRemaining.ToString("hh\:mm\:ss")
Notice how format string is different for TimeSpan, compared to formatting a date time, for example, and that : now requires escaping. This is explained in detail in below link #2.
References:
Standard TimeSpan Format Strings # MSDN
Custom TimeSpan Format Strings # MSDN
Let's stop here for a second, while I try to explain why it did not work for you. Forget about ticks, think in seconds, because it's a measurable interval, that's easy to get a grasp on. Suppose you time interval is a second. Now you are trying to create a date, passing one second into it. What do you expect to get? 1 second AD, i.e. 1st year, 1st month etc.? Fair enough.
Suppose now you have an interval of minus 1 second (yes, intervals can be negative). You would think it's 1 second BC, right? Unfortunately, negative dates in .NET are not allowed.
As a general rule of thumb, intervals of time (represented in .NET by a TimeSpan), and points in time (represented by a DateTime) should be treated separately, because they are logically different entities. There is one-way relation though, i.e. two dates can represent a TimeSpan. However, a TimeSpan does not represent two dates. In fact, no matter how many TimeSpans you have, you will never be able to relate them to any point in time.
Related
I have a case where data presented on the chart is gathered in five minutes interval during the working hours, while at night it stays idle. When the chart presents data from a couple of days, there are obviously a flat lines between days (during nights).
Using category axis solves the problem with long steady lines during nights. However using time axis I was able to format date and use time interval property.
How can I do that using category axis? I mean formatting tick labels and achieve similar behaviour to using time intervals?
That is how it looks like at the moment using category axis:
I would like to present just one tick for every day, properly formatted.
Thank you in advance for any help.
You can do this by using a grouped category axis, It will involve a little manipulation of your dates into a separate time part and day part using some code like this:
// Create a d3 parser for this particular date format, this may not be relevant
// if you already have actual dates
var inFormat = d3.time.format("%Y-%m-%d %H:%M");
// Create a d3 parser for the day and time formats we are going to use
var dayFormat = d3.time.format("%d %b"),
timeFormat = d3.time.format("%H:%M");
// Add some code to manipulate the date into 2 separate fields
// because this is a category axis you need to handle formatting here
// category axes use text content
data.forEach(function (d) {
// Convert the date to an actual date, you may not need to do this if
// you already have date objects in your data
var inDate = inFormat.parse(d.Date);
// Add a new field for the time portion
d["Day"] = dayFormat(inDate);
d["Time"] = timeFormat(inDate);
}, this);
Here it is in context doing what I think you want:
http://jsfiddle.net/87GHM/1/
I have spent quite a few hours and still unable to understand this:
Dim unix_time_at_midnight As Long
DateTime.DateFormat = "MM/dd/yyyy"
unix_time_at_midnight = DateTime.DateParse(DateTime.Date(unix_time*1000))/1000
where both unix_time_at_midnight and unix_time are long values. I understand DateTime.DateParse excepts a String and converts it to DateTime. What is (DateTime.Date(unix_time*1000))/1000 returning and what is its equivalent in Java? The requirement is to get the number of seconds since GMT midnight and I have successfully implemented it in Java. However, I would like to understand this particular line of code written in VB.net
EDIT: This method was written in Basic4Android and probably constitutes more of its libraries then vb.net. However, I have looked into each for details but unable to understand. Would appreciate if you could elaborate. Please see the links.
Take this:
DateTime.Date(unix_time*1000)
The documentation says:
Date (Ticks As Long) As String
Returns a string representation of the date (which is stored as ticks).
The date format can be set with the DateFormat keyword.
So that part returns a string representing the date.
It then uses DateTime.DateParse, which is documented as:
DateParse (Date As String) As Long
Parses the given date string and returns its ticks representation.
Taken together, this appears to take the ticks, multiplied by 1000, converted to a string that doesn't contain hour information which is parsed back to ticks which are divided by 1000.
The important thing to note is that the DateFormat set on the line before contains only the formatting for the date, no hours/minutes/seconds and smaller units of time exist in it. This means that the string returned essentially represents midnight of that date.
I have had a quick look around and I can't find an answer to this in vb.net or something that I can convert into vb.net.
I have two DateTimes in vb.net's 'Date' class. I would like to find the difference between these in seconds. I can do a-b, but the answer will still be a 'date'. I can use .seconds .minutes etc. and multiply but I will hit problems when I come to months.
Is there a simple way to do this, or do I need to write some elaborate-ish code?
Many Thanks
Subtract the DateTime values from each other - the returned type will be a TimeSpan.
Get the TotalSeconds value from it.
(date1 - date2).TotalSeconds
There actually is also a function made for this DateDiff(DateInterval.Second, d1, d2)
How about something like
Dim secs As Double = DateTime.Today.Subtract(DateTime.Today.AddDays(-1)).TotalSeconds
Look at DateTime.Subtract Method (DateTime) and TimeSpan.TotalSeconds Property
I'm looking to set the y-axis for a MSChart to be midnight to midnight, in either regular time format(i.e. - "1:30 AM") or military time. I figured out I can specify the y-axis format using ChartArea.AxisY.LabelStyle = new LabelStyle() { Format = "HH:mm" }, but cannot figure out what to set the minimum/maximum values to be.
Has anyone employed this format?
There are a few things you need to do to get this working properly.
MSChart accepts DateTime objects as Y values. You can emulate durations by doing this for each of your data points (assuming they are timespans or something convertible into a TimeSpan):
TimeSpan testSpan = TimeSpan.FromMinutes(5);
YourChart.Series(0).Points.AddY(new DateTime(testSpan.Ticks))
That will convert it into a datetime starting from the the beginning of CLR time (e.g. 1/1/0001 12:05:00 AM).
Then just use the label format "HH:mm" on the Y-axis.
<asp:ChartArea Name="VsChartArea">
<AxisY Minimum="0">
<LabelStyle Format="HH:mm" />
</AxisY>
</asp:ChartArea>
That should make it look like this:
To setup a custom interval (5 minutes):
<AxisY Minimum="0" IntervalType="Minutes" Interval="5">
Hope this helps!
I found a workaround since I never could get the formatting to work natively with DateTime values.
I eventually changed my Y axis data to be in integer format, with ranges from 0 to 2400(2359 really) to represent military time. I then updated the LabelStyle.Format to be "00:00" which renders my integer values into military time.
Yay for me. Hope this helps someone else.
If you use the Military time, do you notice that your scale is nolonger linear?
I want to display a "date/time submitted" value much the same way as Stack Overflow does
e.g.* 2 hours ago
* 3 days ago
* a month ago
I see extensive answers on how to do this in PHP but can someone help me with the VB version?
First calculate the exact time since submission:
Dim t As TimeSpan = DateTime.Now - submittedTime
Then you decide on what intervals you want to use for displaying the result. For example:
If t.TotalSeconds < 60 Then
display = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
display = t.Minutes.ToString() + " minutes ago"
ElseIf ...
... and so on
End If
All the way back on question 11, Jeff posted the code they use here on stackoverflow. It's in c#, but the conversion to vb.net should be pretty easy. There's heaps of other good suggestions in that question too.
Personally, I've used Sam Allen's code to get prettydates before. He modeled it off some code that John Resig wrote, so it's good quality stuff. Once again, c#, but it converts to vb.net pretty easily.
You need a DateTime structure
you simply substract [now] - [original message time] = difference time
you will need the DateTime.Substract method
What you're looking for is the TimeSpan structure. This structure stores a span of time (as the name suggests). It is the type returned when you subtract two DateTime structures
Dim start as DateTime = DateTime.Now
...
' Some time after the start
Dim span = DateTime.Now - start
I've posted a blog post to do just this. However it is in C#, but you can easily convert into vb.net
http://blog.nirandas.com/post/displaying-time-in-relative-format.aspx