Date Control

// Creates a date control that uses YYYY-MM-DD format.
// Sticking to one standard date format simplifies life immensely.
// You can enter a date directly in YYYY-MM-DD format. You can also use up/down
// buttons. And you can also use the mouse scroll wheel.
// Two SetVariable controls are used - one to display the date and to accept a date string
// and the other to provide the up/down buttons.
// Does not support fractional dates or date and time.
// To try it, execute:
//  DemoDateControlPanel()

static Constant kDebugDateControl = 0   // Set to 1 for debug printouts

Function ConvertYYYYMMDDToIgorDate(dateStr)
    String dateStr      // In YYYY-MM-DD format

    String separatorStr = "-"

    Variable month, day, year
    String formatStr = "%d" + separatorStr + "%d" + separatorStr + "%d"
    sscanf dateStr, formatStr, year, month, day
    Variable dt = date2secs(year, month, day )
    return dt
End

Function/S SetDateControlDate(panelName, controlName, igorDateTimeValue)
    String panelName
    String controlName
    Variable igorDateTimeValue      // In Igor date/time format (seconds since 1904)
   
    String dateStr = Secs2Date(igorDateTimeValue, -2)
    SetVariable $controlName win=$panelName, value=_STR:dateStr
    return dateStr
End

// DateUpDownSetVarProc(sva)
// This is the action procedure for the SetVariable control that provides the
// up and down buttons.
Function DateUpDownSetVarProc(sva) : SetVariableControl
    STRUCT WMSetVariableAction &sva

    switch( sva.eventCode )
        case 1: // mouse up
        case 3: // Live update
            Variable dval = sva.dval
            ControlInfo DateStringSetVar
            String dateStr = S_value        // e.g., "2011-06-19"
            Variable igorDateTimeValue = ConvertYYYYMMDDToIgorDate(dateStr)
            igorDateTimeValue += dval * 86400
            dateStr = SetDateControlDate(sva.win, "DateStringSetVar", igorDateTimeValue)
            SetVariable DateUpDownSetVar value=_NUM:0
            if (kDebugDateControl)
                if (dval > 0)
                    Printf "Up button; Result = %s (Igor date/time value=%d)\r", dateStr, igorDateTimeValue
                else
                    Printf "Down button; Result = %s (Igor date/time value=%d)\r", dateStr, igorDateTimeValue
                endif
            endif
            break
    endswitch

    return 0
End

// DateStringSetVarProc(sva)
// This is the action procedure for the SetVariable control that displays the
// date and accepts manually-entered dates.
Function DateStringSetVarProc(sva) : SetVariableControl
    STRUCT WMSetVariableAction &sva
   
    Variable igorDateTimeValue
    String dateStr

    switch( sva.eventCode )
        case 1: // mouse up
            Printf "Mouse up: text = %s\r", sva.sval
            break
           
        case 2: // Enter key
            igorDateTimeValue = ConvertYYYYMMDDToIgorDate(sva.sval)
            dateStr = Secs2Date(igorDateTimeValue, -2)
            if (kDebugDateControl)
                Printf "Enter key: Result = %s (Igor date/time value=%d)\r", dateStr, igorDateTimeValue
            endif
            break
           
        case 4: // Scroll wheel up
            igorDateTimeValue = ConvertYYYYMMDDToIgorDate(sva.sval)
            igorDateTimeValue += 86400      // Add one day
            dateStr = SetDateControlDate(sva.win, sva.ctrlName, igorDateTimeValue)
            if (kDebugDateControl)
                Printf "Scroll wheel up: Result = %s (Igor date/time value=%d)\r", dateStr, igorDateTimeValue
            endif
            break
           
        case 5: // Scroll wheel down
            igorDateTimeValue = ConvertYYYYMMDDToIgorDate(sva.sval)
            igorDateTimeValue -= 86400      // Subtract one day
            dateStr = SetDateControlDate(sva.win, sva.ctrlName, igorDateTimeValue)
            if (kDebugDateControl)
                Printf "Scroll wheel down: Result = %s (Igor date/time value=%d)\r", dateStr, igorDateTimeValue
            endif
            break
    endswitch

    return 0
End

Window DemoDateControlPanel() : Panel
    PauseUpdate; Silent 1       // building window...
    NewPanel /W=(197,60,639,265) /K=2 as "Demo Date Control"
   
    SetVariable DateStringSetVar,pos={1,2},size={150,19},live=1,proc=DateStringSetVarProc,title="Date:"
    SetVariable DateStringSetVar,fSize=12,value= _STR:"2011-06-19"
   
    Variable hintVPos = 2                   // Works well on Macintosh
    #ifdef WINDOWS
        hintVPos = 3                        // Works better on Windows
    #endif
    TitleBox DateHint,pos={171,3},size={85,16},title="(YYYY-MM-DD)",fSize=12,frame=0
   
    Variable upDownSetVarWidth
    Variable upDownSetVarLeft
    if (CmpStr("Windows",IgorInfo(2)) == 0)
        upDownSetVarLeft = 149          // Works better on Windows
        upDownSetVarWidth = 14          // Works better on Windows
    else
        upDownSetVarLeft = 152          // Works better on Macintosh
        upDownSetVarWidth = 18          // Works well on Macintosh
    endif
    SetVariable DateUpDownSetVar,pos={upDownSetVarLeft,2},size={upDownSetVarWidth,19},proc=DateUpDownSetVarProc,fSize=12
    SetVariable DateUpDownSetVar,value= _NUM:0
EndMacro

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More