Page 1 of 1
Converting date string into date format
Posted: Sun May 19, 2019 9:36 am
by Silvio.Falconi
I have astring as this
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
Re: Converting date string into date format
Posted: Sun May 19, 2019 3:44 pm
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
Re: Converting date string into date format
Posted: Sun May 19, 2019 4:31 pm
by ukoenig
tested and works
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
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
Re: Converting date string into date format
Posted: Mon May 20, 2019 6:46 am
by Silvio.Falconi
thanks to all
Re: Converting date string into date format
Posted: Mon May 20, 2019 9:05 am
by ukoenig
Much better
( 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
Re: Converting date string into date format
Posted: Mon May 20, 2019 9:46 am
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" }
Re: Converting date string into date format
Posted: Mon May 20, 2019 10:17 am
by ukoenig
With language-support
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 )
Re: Converting date string into date format
Posted: Wed May 22, 2019 11:24 am
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
Re: Converting date string into date format
Posted: Wed May 22, 2019 3:29 pm
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
uCharToVal( "Monday, 04 March 2019", "D" )
works well with English only.
From FWH1904 onwards, it will work with other codepage languages.
Re: Converting date string into date format
Posted: Wed May 22, 2019 3:58 pm
by Silvio.Falconi
thanks