Converting date string into date format

Converting date string into date format

Postby Silvio.Falconi » Sun May 19, 2019 9:36 am

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  Expand view  RUN
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
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7074
Joined: Thu Oct 18, 2012 7:17 pm

Re: Converting date string into date format

Postby Rick Lipkin » Sun May 19, 2019 3:44 pm

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
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Converting date string into date format

Postby ukoenig » Sun May 19, 2019 4:31 pm

tested and works :D
don't use AT and substrings :!:

SomeOne can publish a easy function to show number of days beetwen two dates ?

viewtopic.php?f=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  Expand view  RUN

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
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Converting date string into date format

Postby Silvio.Falconi » Mon May 20, 2019 6:46 am

thanks to all
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7074
Joined: Thu Oct 18, 2012 7:17 pm

Re: Converting date string into date format

Postby ukoenig » Mon May 20, 2019 9:05 am

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

Code: Select all  Expand view  RUN

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
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Converting date string into date format

Postby Silvio.Falconi » Mon May 20, 2019 9:46 am

{ "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" }
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7074
Joined: Thu Oct 18, 2012 7:17 pm

Re: Converting date string into date format

Postby ukoenig » Mon May 20, 2019 10:17 am

With language-support :wink:

Code: Select all  Expand view  RUN

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: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Converting date string into date format

Postby ukoenig » Wed May 22, 2019 11:24 am

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
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Converting date string into date format

Postby nageswaragunupudi » Wed May 22, 2019 3:29 pm

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
nageswaragunupudi
 
Posts: 10662
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Converting date string into date format

Postby Silvio.Falconi » Wed May 22, 2019 3:58 pm

thanks
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7074
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 47 guests