Solved

Date Difference and display format: Format: yyyy-MM-dd HH:mm:SS.xxx

Posted on 2010-11-19
6
1,415 Views
Last Modified: 2012-05-10
I need to Display the Current date and time - (minus)1 hours to the following format: Format: yyyy-MM-dd HH:mm:SS.xxx

can someone help me?

Thank you in advance.
0
Comment
Question by:vico1
6 Comments
 
LVL 8

Expert Comment

by:Den_HBR
ID: 34177098
MsgBox(Format(DateAdd(DateInterval.Hour, -1, Now), "yyyy-MM-dd HH:mm:ss.fff"))
0
 
LVL 9

Assisted Solution

by:JonMny
JonMny earned 50 total points
ID: 34177151


mydate=DateAdd("h",1,Now)
MsgBox DatePart("yyyy",mydate) & "-" & DatePart("m",mydate) & "-" & DatePart("d",mydate) & " " & DatePart("h",mydate) & ":" &  DatePart("n",mydate) & ":" &  DatePart("s",mydate)  

0
 
LVL 22

Assisted Solution

by:danaseaman
danaseaman earned 50 total points
ID: 34178022
For 10ms accuracy you can use Timer.
For  1 ms accuracy you need to use API GetSystemTime.

Sample code for both methods:
Option Explicit



Private Type SYSTEMTIME

        wYear As Integer

        wMonth As Integer

        wDayOfWeek As Integer

        wDay As Integer

        wHour As Integer

        wMinute As Integer

        wSecond As Integer

        wMilliseconds As Integer

End Type



Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)



Private Sub Form_Load()

   Dim typTime As SYSTEMTIME

   

   GetSystemTime typTime

   Debug.Print Format$(DateAdd("h", -1, Now), "yyyy-MM-dd HH:mm:ss.") & Format$(typTime.wMilliseconds, "000"), " 1 Millisecond accuracy"

   Debug.Print Format$(DateAdd("h", -1, Now), "yyyy-MM-dd HH:mm:ss.") & Right$(Format$(Timer, "#0.000"), 3), "10 Millisecond accuracy"

End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Accepted Solution

by:
Den_HBR earned 400 total points
ID: 34179924
I'm sorry.
Didn't saw it was for asp
<%@ LANGUAGE="VBSCRIPT" %>

<HTML>

<HEAD>

<TITLE>ASP Test Page</TITLE>

<%

Function GetCurrentDateAndTimeMinus1Hour()

	Dim timeStamp

        Dim dateStamp

	timeStamp = Timer

	dateStamp = Now()

	if timeStamp < 3600 then

            dateStamp = DateAdd("h", -1, Now) 'substract a day

	    timeStamp = timeStamp + 86400     'add a day	

	end if

	timeStamp = timeStamp - 3600

	Dim dateAndTime

	'year

	dateAndTime = dateAndTime & DatePart("yyyy",dateStamp)

	dateAndTime = dateAndTime & "-"

	'month

        dateAndTime = dateAndTime & right("0" & DatePart("m",dateStamp),2)

	dateAndTime = dateAndTime & "-"

	'day

        dateAndTime = dateAndTime & right("0" & DatePart("d",dateStamp),2)

	dateAndTime = dateAndTime & " "	

        'hour

        dateAndTime = dateAndTime & right("0" & Fix(timeStamp / 3600),2)

	dateAndTime = dateAndTime & ":"

        'minute

        timeStamp = timeStamp - (Fix(timeStamp / 3600)*3600)

        dateAndTime = dateAndTime & right("0" & Fix(timeStamp / 60),2)

	dateAndTime = dateAndTime & ":"

        'second

        timeStamp = timeStamp - (Fix(timeStamp / 60)*60)

        dateAndTime = dateAndTime & right("0" & Fix(timeStamp),2)

	dateAndTime = dateAndTime & "."

        'millisecond

        timeStamp = Fix((timeStamp - Fix(timeStamp))*1000)

        dateAndTime = dateAndTime  & right("00" & timeStamp ,3)

	

	GetCurrentDateAndTimeMinus1Hour = dateAndTime

End Function

%> 



</HEAD>



<BODY>

<%=GetCurrentDateAndTimeMinus1Hour %>

</BODY>

</HTML>

Open in new window

0
 
LVL 15

Author Comment

by:vico1
ID: 34180110
Different versions,
Den's will work best for me.
Thank you all for the quick response.

Vico1
0
 
LVL 15

Author Closing Comment

by:vico1
ID: 34180119
Thank you Den HBR!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now