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
Related
I Have been playing with variations of the Month... function in access query builder. I am having trouble building a date value from an expression. I am looking to create my own date that will be behind the scenes to perform some filtering and other tasks. My problem is that I cant seem to get the Month(number) function to do what I think it should be doing. Here is a summary of what I am looking for.
5/31/2012
Through something like this
DateSerial(Year(Date()),Month(5),Day(31))
Also
DateSerial(Year(Date()),Month("5"),Day("31"))
When I try these as an experssion the return is
1/30/2012
Im sure I am misunderstanding the structure. Please educate me.
DateSerial requires three integers, year, month, day:
DateSerial(1992,5,2)
02/05/1992 ''Euro locale
Year(Date()) returns an integer, so you can substitute:
DateSerial(Year(Date()),5,31)
Interestingly, the zeroth day is the last day of the previous month:
DateSerial(2012,12,0)=30/11/2012
-- http://office.microsoft.com/en-ie/access-help/HV080206953.aspx
As an aside, do not forget that all dates are numbers.
Month(5) will equal 1, but Month(41263)=12 !
Also
?month(100)
4
?Year(100)
1900
I am trying to take the following value and making it into a date format of dd-MMM-yy
For example: 110412 turns into 04-NOV-12
My formula as of right now is:
(CONCATENATE(MID(E14,3,2),"-",(TEXT(LEFT(E14,2),"MMM")),"-",RIGHT(E14,2)))
It is giving me 04-Jan-12.
Please note that I would like the month to be in all caps.
Try using the "UPPER(foo)" function as a wrapper to make the result all caps.
UPPER((CONCATENATE(MID(E14,3,2),"-",(TEXT(LEFT(E14,2),"MMM")),"-",RIGHT(E14,2))))
or better still:
=MID(E14,3,2)&"-"&UPPER(TEXT(DATE(MID(E14,5,2),MID(E14,1,2),MID(E14,3,2)),"MMM"))&"-"&RIGHT(E14,2)
I would like to get today's date in the following format: mmddyyyy.
Even if it's first of August I still want zeros in front, like so: 08012012.
I think I can use DatePart function for it, but I am not sure how.
Anyone has an idea?
Thanks.
You do not say which application, but in VBA, you can say:
Format(date(),"mmddyyyy")
Application.Text(Date(),"mmddyyyy")
Format(NOW,"mmddyyyy")
NOW gives time as well:
Format(NOW,"hh:mm:ss")
guys
I have two question to ask, they're easy, but bothering me for a while.
I access my .NET test WebService, and it return two parameters to me.
One is a date data just like "/Date(1332399761677+0800)/", and I dont know
how to format it to the normal date format.
Two is a NSString data looks like "12000.00000",and I want to change it to
the format like this:"12000.00".
So,please help me with this two problems. Thank you in advance.
1332399761677 looks like a Unix date, so if you grab that part of the string, use NSString doubleValue to turn it into a double, then use NSDate dateWithTimeIntervalSince1970:, you should be able to get a date. +0800 looks like a timezone, but you wouldn't need the timezone to get the date given a Unix date: 1332399761677 would specify a specific point in time, irrespective of timezones.
As for "12000.00000", you would use doubleValue to make it into a double, make an NSNumberFormatter with its maximumFractionDigits and minimumFractionDigits set to 2, then use stringFromNumber:.
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