Generate filename based on current date & time.

Post Reply
User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Generate filename based on current date & time.

Post by Dav »

This little snippet generates a filename based on the current date and time. Can be useful for apps that save series of files, like an image saver, or text saving program. An example is:: 2015-08-01-09-12-50.txt

I use it for a notepad app (that's why I added .txt to the end of filename). Maybe someone can use it.

- Dav

Code: Select all

'datefilename.txt
'generates a filename based on date & time
'example: 2015-08-01-09-12-50.txt
'by Dav


print datename$

end


def datename$()
  'make a name based on date & time
  'example: 2015-08-01-09-12-50.txt
  fd$=current_year()&"-"
  fd$=fd$&pad$(str$(current_month()))&"-"
  fd$=fd$&pad$(str$(current_date()))&"-"
  fd$=fd$&pad$(str$(current_hour()))&"-"
  fd$=fd$&pad$(str$(current_minute()))&"-"
  fd$=fd$&pad$(str$(current_second()))&".txt"
  datename$=fd$
end def
def pad$(a$)
  'adds leading 0 to string if len=1
  if len(a$)=1 then pad$="0"&a$ else pad$=a$
end def

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Generate filename based on current date & time.

Post by Mr. Kibernetik »

It is not necessary to convert number to string with str$() function when combining strings.
For example it is correctly to write:

a$ = 5 & " and " & 3

Smart BASIC will understand what you want to get.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Generate filename based on current date & time.

Post by rbytes »

I think this will be a lot more useful than my current system, which just numbers the files so they don't overwrite each other. Thanks.
The only thing that gets me down is gravity...

Post Reply