User Tools

Site Tools


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).




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

Print Now(1);“ system startup”

If Daylight(Now()) Then …

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


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

		If opt Then
		End If
	End Function

See Also: DateAdd()

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