Converting date string into date format

Post Reply
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Converting date string into date format

Post by Silvio.Falconi »

I have astring as this

Image


and I wish converte it into a date value
I tried with this function but I not Know how found the mounth

Code: Select all

 Function StringtoDate(dString)
    Local dTemp
    Local nPos:= At(",",dString)
    Local cStringDate:= SubStr(dString,nPos+1)
    Local dDay:=  SubStr(cStringDate,At(" ",cStringDate)+1,2)   //found the day
    Local dMounth:= SubStr(cStringDate,At(dDay,cStringDate)+2)    // found the mouth + year 
    Local dYear := Right(dMounth, 4)                                                           //found the year 

    ? dday
    ? dMounth
    ? dYear
        * ctod(day
    RETURN dTemp

 




someone can Help me please
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Converting date string into date format

Post by Rick Lipkin »

Silvo

It appears there is a space between the Month and year dMonth := "Febbraio 2014"

nPos := At( dMonth , " " ) // should equal position 9
cMonth := substr(dMonth,1,(nPos-1)) // should equal "Febbraio"

Basically .. find the position of the blank between the month and year .. then substr the string 1, nPos-1

Un-tested,but should work..

Rick Lipkin
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Converting date string into date format

Post by ukoenig »

tested and works :D
don't use AT and substrings :!:
SomeOne can publish a easy function to show number of days beetwen two dates ?
http://forums.fivetechsupport.com/viewt ... =3&t=37185

replace the month.names with Your language ( converted to numeric month )

first line shows the original datestrings
second line = result
third line the converted date-strings and calculation

Image

Code: Select all

dDat2 := CONV_DTEXT( "Dienstag, 05 März 2019" )
dDat1 := CONV_DTEXT( "Montag, 25 Dezember 2019" )

cResult := "25 Dezember 2019 - 05 März 2019" + CRLF + ;
                  + CRLF + STR( dDat1 - dDat2 ) + " days" + CRLF + CRLF + DTOC( dDat1 ) + ;
          "  -  " + DTOC( dDat2 ) 

MsgAlert( cResult, "Days between 2 date-strings" )

// -----------------

FUNCTION CONV_DTEXT( cDate )
   
cText := ""
I := 2
FOR I := 2 TO 4
    IF I < 4
        IF I = 2
            cText += LTRIM( StrToken( cDate, I, " " ) ) + "." // Day
        ELSEIF I = 3
            IF StrToken( cDate, 3, " " ) = "Januar"  // convert month to number
                cMonth := "01"
            ELSEIF StrToken( cDate, 3, " " ) = "Februar"
                cMonth := "02"
            ELSEIF StrToken( cDate, 3, " " ) = "März"
                cMonth := "03"
            ELSEIF StrToken( cDate, 3, " " ) = "April"
                cMonth := "04"
            ELSEIF StrToken( cDate, 3, " " ) = "Mai"
                cMonth := "05"
            ELSEIF StrToken( cDate, 3, " " ) = "Juni"
                cMonth := "06"
            ELSEIF StrToken( cDate, 3, " " ) = "Juli"
                cMonth := "07"
            ELSEIF StrToken( cDate, 3, " " ) = "August"
                cMonth := "08"
            ELSEIF StrToken( cDate, 3, " " ) = "September"
                cMonth := "09"
            ELSEIF StrToken( cDate, 3, " " ) = "Oktober"
                cMonth := "10"
            ELSEIF StrToken( cDate, 3, " " ) = "November"
                cMonth := "11"
            ELSEIF StrToken( cDate, 3, " " ) = "Dezember"
                cMonth := "12"
            ENDIF
                        cText += cMonth + "."               // Month
        ENDIF
    ELSE
        cText += LTRIM( StrToken( cDate, I, " " ) )  // Year
    ENDIF
NEXT

RETURN( cTOD( cText ) )
 
regards
Uwe :D
Last edited by ukoenig on Mon May 20, 2019 6:57 pm, edited 2 times in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Converting date string into date format

Post by Silvio.Falconi »

thanks to all
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Converting date string into date format

Post by ukoenig »

Much better :idea: ( translated and improved using a month-array )
If needed the day can be extracted as well

Code: Select all

dDat2 := CONV_DTEXT( "Monday, 04 March 2019" )
dDat1 := CONV_DTEXT( "Tuesday, 26 December 2019" )

cResult := "26 December 2019 - 04 March 2019" + CRLF + ;
                  + CRLF + STR( dDat1 - dDat2 ) + ;
          " days counted" + CRLF + CRLF + ;
          "converted to : " + DTOC( dDat1 ) + "  -  " + DTOC( dDat2 ) 

MsgAlert( cResult, "Days" )

// -----------------

STATIC FUNCTION CONV_DTEXT( cDate )
LOCAL cText := "", I := 2, cMonthName := ""

FOR I := 2 TO 4
     IF I < 4
          IF I = 2
               cText += LTRIM( StrToken( cDate, I, " " ) ) + "."   // Day
          ELSEIF I = 3
               cMonthName := StrToken( cDate, 3, " " )
              // MsgAlert(cMonthName, "month-name from string" )
              cText += MONTH_CONV(cMonthName) + "."        
          ENDIF
     ELSE
          cText += LTRIM( StrToken( cDate, I, " " ) ) // Year
     ENDIF
NEXT

RETURN( cTOD( cText ) )

// ------------

STATIC FUNCTION MONTH_CONV( cMonthName )
LOCAL aMonth :={}, cMonthConv := "  "
   
AADD( aMonth, { "January", "01" } )
AADD( aMonth, { "February", "02" } )
AADD( aMonth, { "March", "03" } )
AADD( aMonth, { "April", "04" } ) 
AADD( aMonth, { "Mai", "05" } )
AADD( aMonth, { "June", "06" } )   
AADD( aMonth, { "July", "07" } )
AADD( aMonth, { "August", "08" } )   
AADD( aMonth, { "September", "09" } )
AADD( aMonth, { "October", "10" } )   
AADD( aMonth, { "November", "11" } )
AADD( aMonth, { "December", "12" } )

FOR I := 1 TO 12
    IF aMonth[I][1] = cMonthName
        cMonthConv := aMonth[I][2]
    ENDIF
NEXT
// MsgAlert(cMonthSel, "converted month" )

RETURN( cMonthConv )
 
regards
Uwe :D
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Converting date string into date format

Post by Silvio.Falconi »

{ "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre" }
{ "January", "Febrary", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" }
{ "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Converting date string into date format

Post by ukoenig »

With language-support :wink:

Code: Select all

dDat2 := CONV_DTEXT( "Monday, 04 March 2019", 1 ) // 1 = english selected
dDat1 := CONV_DTEXT( "Tuesday, 26 December 2019", 1 )

cResult := "26 December 2019 - 04 March 2019" + CRLF + ;
                  + CRLF + STR( dDat1 - dDat2 ) + ;
          " days counted" + CRLF + CRLF + ;
          "converted to : " + DTOC( dDat1 ) + "  -  " + DTOC( dDat2 ) 

MsgAlert( cResult, "Days" )

// -----------------

STATIC FUNCTION CONV_DTEXT( cDate, nLanguage )
LOCAL cText := "", I := 2, cMonthName := ""

FOR I := 2 TO 4
    IF I < 4
        IF I = 2
            cText += LTRIM( StrToken( cDate, I, " " ) ) + "."   // Day
        ELSEIF I = 3
            cMonthName := StrToken( cDate, 3, " " )
            // MsgAlert(cMonthName, "month-name from string" )
            cText += MONTH_CONV( cMonthName, nLanguage ) + "."       
        ENDIF
    ELSE
        cText += LTRIM( StrToken( cDate, I, " " ) ) // Year
    ENDIF
NEXT

RETURN( cTOD( cText ) )

// ------------

STATIC FUNCTION MONTH_CONV( cMonthName, nLanguage )
LOCAL aMonth :={}, cMonthConv := "  ", I := 1
   
AADD( aMonth, { "January", "Januar", "Gennaio", "Enero", "01" } )
AADD( aMonth, { "February"  , "Februar", "Febbraio", "Febrero", "02" } )
AADD( aMonth, { "March", "März", "Marzo", "Marzo", "03" } )
AADD( aMonth, { "April" , "April", "Aprile", "Abril", "04" } ) 
AADD( aMonth, { "Mai", "Mai", "Maggio", "Mayo", "05" } )
AADD( aMonth, { "June", "Juni", "Giugno", "Junio", "06" } )   
AADD( aMonth, { "July", "Juli", "Luglio", "Julio", "07" } )
AADD( aMonth, { "August", "August", "Agosto", "Agosto", "08" } )   
AADD( aMonth, { "September", "September", "Settembre", "Septiembre", "09" } )
AADD( aMonth, { "October", "Oktober", "Ottobre", "Octubre", "10" } )   
AADD( aMonth, { "November", "November", "Novembre", "Noviembre", "11" } )
AADD( aMonth, { "December", "Dezember", "Dicembre", "Diciembre", "12" } )

FOR I := 1 TO 12
    IF aMonth[I][nLanguage] = cMonthName
        cMonthConv := aMonth[I][5]
    ENDIF
NEXT
// MsgAlert(cMonthSel, "converted month" )

RETURN( cMonthConv )

 
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
Posts: 3981
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Converting date string into date format

Post by ukoenig »

Silvio,

there is a function ( seems to work as well )

// can now convert date text containing month names also into valid dates.
MsgAlert( uCharToVal( "Monday, 04 March 2019" ) )


regards
Uwe :D
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Converting date string into date format

Post by nageswaragunupudi »

ukoenig wrote:Silvio,

there is a function ( seems to work as well )

// can now convert date text containing month names also into valid dates.
MsgAlert( uCharToVal( "Monday, 04 March 2019" ) )


regards
Uwe :D
uCharToVal( "Monday, 04 March 2019", "D" )
works well with English only.
From FWH1904 onwards, it will work with other codepage languages.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Converting date string into date format

Post by Silvio.Falconi »

thanks
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
Post Reply