Notifications
Clear all

[Closed] Local Time

How can I structure this string so that it will create a folder that also has the Day, Month, and Year in the folder name? here is my code:


 getLocalTime()
 			year = localTime.year
 			month = localTime.month
 			day = localTime.day
 			makeDir (txtSavePath.text + "\\" + txtLocation.text + "_" + cameraName + "_" + year + "_" + month + "_" + day)
 

I think just from reading this you can see what Im trying to achieve… My problem is getting the Day, Month and Year to create properly… What I am doing wrong?

8 Replies

LocalTime simply gives you a string with the date and time. It is also dependent on the Windows regional settings and is thus difficult to parse correctly.

In Max 2008 and higher, you also have the function [b]getLocalTime/b.
It will return an array of values in the form you need:

[left]1. Year – Specifies the current year.
[/left]
[left]2. Month – Specifies the current month; January = 1, February = 2, and so on.
[/left]
[left]3. DayOfWeek – Specifies the current day of the week; Sunday = 0, Monday = 1, and so on.
[/left]
[left]4. Day – Specifies the current day of the month.
[/left]
[left]5. Hour – Specifies the current hour.
[/left]
[left]6. Minute – Specifies the current minute.
[/left]
[left]7. Second – Specifies the current second.
[/left]
[left]8. Milliseconds – Specifies the current millisecond.
[/left]

If you are using Max 9 or earlier, this function was available in the Avguard.DLX extension available from well-sorted plugin sites like www.maxplugins.de

Bobo,
Yes, i saw this in the maxscript reference. But my structuring of the code is where my problem is…

Im using getLocalTime()

but to access the year, month and day Im coding it incorrectly.
I’ve tried:


 getLocalTime()
 year as string
 

 getLocalTime()
 year = localTime.year
 

etc.

What do I need to do so that my code will read correctly? I think im drawing a blank on calling functions… i dont know

Making progress, but still problems, this is what my code looks like now:


getLocalTime()
			year = year as string
			month = month as string
			day = day as string
			makeDir (txtSavePath.text + "\\" + txtLocation.text + "_" + cameraName + "_" + year + "_" + month + "_" + day)

But the folder being created says:
Test_Camera01_undefined_undefined_undefined

Apparently year, month and day are still undefined, but the folder is creating… What am I doing wrong?

First a demonstration…

global MyTimeArray = getLocalTime()
#(2008, 9, 4, 4, 19, 57, 7, 109)
for i=1 to MyTimeArray.count do (format "Array Member %: %
" (i as string) (MyTimeArray[i] as string))
Array Member 1: 2008
Array Member 2: 9
Array Member 3: 4
Array Member 4: 4
Array Member 5: 19
Array Member 6: 57
Array Member 7: 7
Array Member 8: 109
OK
TheYear = MyTimeArray[1]
2008
TheMonth = MyTimeArray[2]
9
TheDay = MyTimeArray[3]
4
((TheMonth as string) + "-" + (TheDay as string) + "-" + (TheYear as string))
"9-4-2008"
TheYear = (MyTimeArray[1] as string)
"2008"

…and now an explanation.

A common purpose of many functions is to return a value that you are expected to store to a variable, which you can then access over and over without calling the function each time. The first user-defined variable (MyTimeArray) is explicitly declared as global, while the others (TheYear, TheMonth, TheDay) are implicitly declared. Implicit declaration is a short-hand way to code while you’re testing only…explicit declaration is the way to go once you’re getting serious with a script.

Once the variable stores the array value you want, you can access its members by using index numbers (from 1 to 8 in this case). You can store these member values to other variables or just keep accessing them from the array.

If you need string values (i.e. text), you need to convert them to strings as shown. As shown in the last line, you can also do the integer-to-string conversion when the variables are first accessed from the array.

I completely understand now, It works well, thank you! Quick question, is it possible to format the date and month to look like 09 or 04 instead of 9 and 4 ?? so the date will look like 2008-09-04 instead of 2008-9-4 ?

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Gosh, I am sorry, my PC was busy creating a Preview animation and I could not use Max while answering, otherwise I would have written an example. I somehow expected that everybody knows about arrays already…

In Max 9 and higher, there is the formattedPrint() function which can be used to do all sort of crazy formatting tricks. Keep in mind it is rather advanced thing, just use the examples and you will get what you want. Also note that it does not print, it just converts numeric values to strings based on a formatting pattern.

For example,

formattedPrint 5 format:"02u"
   -->"05"

In the format: string, the 0 means “add leading zeros”, the “2” means “make it two digits”, the “u” means “unsigned decimal integer”.

Thus,

formattedPrint 12 format:"04u"
    -->"0012"

So you could either use this replacing the number with the date value for the month and day, or go farther and create a custom function where you can pass the number and the digit count you want and get back your value:

fn addLeadingZeros theNumber theDigits=
   (
   	formattedPrint theNumber format:("0"+theDigits as string + "u")
   )
   
   theTimeArray = getLocalTime()
   theDate = theTimeArray[1] as string + "-" + addLeadingZeros theTimeArray[2] 2 + "-" + addLeadingZeros theTimeArray[4] 2
   
   -->"2008-09-04"
    

Btw, when I create a folder with the date and time stamp in it, I usually use the old way:


  fn purifiedDate =
  (
  	local theStringArray = FilterString localTime "\\:/ "
  	local theString = ""
  	for i = 1 to theStringArray.count-1 do theString += theStringArray[i] + "_"
  	theString += theStringArray[theStringArray.count]	
  )
  purifiedDate()
  -->"04_09_2008_11_33_27_PM"

But, as I mentioned, this formatting depends on the OS regional settings (in this case, it is set to DD_MM_YYYY)

Bobo,

I understand your delay in replying Thanks for responding! I’ve put together the input from you and John and I’ve come up with a solution that works well! Thanks again for your feedback! Here is what my code looks like that works. (BTW This code is inside a button):


 calendar = getLocalTime()
 			year = calendar[1] as string
 			month = calendar[2] 
 			day = calendar[4] 
 			
 			month = formattedPrint month format:"02u"
 			day = formattedPrint day format:"02u"
 			
 			month as string
 			day as string
 			makeDir (txtSavePath.text + "\\" + txtLocation.text + "_" + cameraName + "_" + year + "_" + month + "_" + day)
 

The variables txtSavePath.text and txtLocation.text are just additional text information thats included in the folder name from textfield boxes…

Glad it worked!
I just noticed the error in my code and fixed it. By a funny coincidence, the Days of the Month this week are the same numbers as the Days Of The Week! So Thursday was the 4th and Friday is the 5th of the month and of the week. Thus I never noticed that I used [3] and not [4] for the day.