User Tools

Site Tools


mmbasic:now_function_vb_work_a_like

Now() Function (VB work-a-like)

Modern languages have extended data types beyond simple strings or numeric. VB has a time datatype which allows addition, subtraction, weekday, month and formatting all with dates as if they were numbers in the conventional sense.

VB's Now() function returns the current system time in a date-type format but if you assign it to a string or simply “print” it, it is converted into DD/MM/YYYY HH:MM:SS which is arguably the most useful format to default to.

Although MMBasic can't hope to support the different datatypes of modern languages, the Now() function is really useful instead of having to construct it each time. This function does that - OK, it isn't rocket science to concatenate the DATE$ and TIME$ but it reduces program size and, I think, makes program flow easier to read.

Now() supports an optional date-format parameter. The default is zero which results in the date following the system format of DD-MM-YYYY. If opt is non-zero, the data part follows the ISO8601format of YYYY-MM-DD. The output string from Now() can be passed directly to the UnixTime function - be sure to maintain the date format option across the two (applies to HumanTime also).

Syntax:

Now([opt])

Examples:

x$=Now() ' same as x$=Now(0)

Print Now(1);“ system startup”

If Daylight(Now()) Then …

Print#3,Unixtime(Now())+“ system halted”

Code:

	Function Now(opt As Integer) As String
		Local t$
		Now=Date$:t$=Time$
		If t$=Time$ Then
			Now=Now+" "+t$
		Else
			Now=Date$+" "+t$
		End If

		If opt Then
			Now=Mid$(Now,7,4)+Mid$(Now,3,4)+Left$(Now,2)+Mid$(Now,11)
		End If
	End Function

See Also: DateAdd()
DateDiff()
DatePart()

mmbasic/now_function_vb_work_a_like.txt · Last modified: 2024/01/19 09:30 by 127.0.0.1