How do you show milliseconds on the time on vb - vb.net

I have a clock on VB and i have got it to say the date and time but it wont show the milliseconds on the time.
time = DateTime.Now.ToString("dd/mm/yy HH:mm:ss")
So I know how to put the hours minuets and seconds but im not sure how to add milliseconds.
Please could someone help.
Thank You

Use fff to represent the milliseconds.
For example:
time = DateTime.Now.ToString("dd/mm/yy HH:mm:ss.fff")
See MSDN for the possible placeholders.

Related

VBA - Decimal to Time - Wrong value no matter what

I'm new to this community so please critizise all you want on this post, if I make any rookie mistakes.
I'm currently stuck on an assignment. I am somewhat new to visual basic, but my understanding so far has been fairly good. However, I can not seem to get a basic operation sorted. I have searched the internet and bashed my head against the wall for a day or two now. Hopefully, you can help me sort this out.
I am importing cycle times for different orders from an Oracle database. They are returned into a column as decimal values (number of hours), but I want them shown as "Long Time" ("hh:mm:ss"). What I have read, it should be as easy as dividing by 24 and changing the format. However, this gives the wrong values when using the cell values.
Example:
? ws.Cells(i,6) 'Cycle times are placed in column 6 while looping over rows i.
0.050000
? Format(0.050000/24, "hh:mm:ss")
00:03:00
but
? Format(ws.Cells(i,6)/24,"hh:mm:ss")
08:00:00
which is clearly not correct. 0.05 hours equivalent to 3 minutes. I have observed a change in date value as well:
? Format(0.05/24, "yyyy-mm-dd hh:mm:ss")
1899-12-30 00:03:00
? Format(ws.Cells(i,6)/24, "yyyy-mm-dd hh:mm:ss")
1905-09-13 08:00:00
How is this even possible!? ws.Cells(i,6) returns 0.05. Despite this Format(ws.Cells(i,6)/24,"hh:mm:ss")≠Format(0.05/24,"hh:mm:ss")
I've tried messing around with number formats (strings, dates etc.), .Value and .Value2
Does anybody know what is going on and why this is happening?
Thank you for your time in advance.
The problem seems to stem from the fact that your computer is thinking that 0.050000 is not actually the number 0.05 but is actually a string that is being interpreted as 50000.
Thus, when you divide 50000 by 12, you get the date/time of 1905-09-13 08:00:00 which is then displayed as just 08:00:00 when a "hh:mm:ss" format is used.
I think you need to check your date/time & language settings to ensure that they are set correctly.

find the difference between two datetimes in seconds

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

Convert epochTime to NSDate

I need to convert an epoch time value to NSDate..
For exemple I have this epoch value (long) : 81915536
I'm interesting by the time value of the epoch value.
I have try that :
NSDate* date = [NSDate dateWithTimeIntervalSince1970:81915536];
but I got a 70's date....It's wrong.
But in a Java when I use this code, I obtain the right time value.
Date time = new Date(Long.valueOf(_epochTime));
String minutes = time.getMinutes()+"";
if(minutes.length()==1)
minutes="0"+minutes;
return time.getHours()+":"+minutes;
Someone can help me with this?
The current UNIX time is 1310407007 the seconds you gave are actually in the seventies, see here: http://www.unixtimestamp.com/index.php.
Also, it doesn't seem that you check the year in your Java code, only the hours and minutes.
One more thing, if you want a constant to be treated as long and not as int, add a postfix of L: 81915536L

Need to set time range on Y axis on a MSChart

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?

How do I nicely display relative time using VB.NET?

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