how to set the dates?

New to FreeBASIC? Post your questions here.
ptitjoz
Posts: 25
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

how to set the dates?

Postby ptitjoz » Apr 07, 2018 6:48

Hello
print date sends me back "04-07-2018"
I would like to have "07/04/2018"
currently I am doing

Code: Select all

datef=mid(date,4,2)&"/"&left(date,2)&"/"&right(date,4)

but can not we set to have the date directly in another format?

regards
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Postby fxm » Apr 07, 2018 8:18

Using NOW and FORMAT (see these two keywords):

Code: Select all

#include "vbcompat.bi"

Print Format(Now, "dd/mm/yyyy")

Sleep
Last edited by fxm on Apr 07, 2018 18:06, edited 1 time in total.
ptitjoz
Posts: 25
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

Re: how to set the dates?

Postby ptitjoz » Apr 07, 2018 13:21

Thanks you very much
it's ok !
Regards.
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Postby MrSwiss » Apr 07, 2018 17:17

Alternative: use your own, customizable Function, which has the advantage, of
not needing, any #Include "whatever.bi" ... (cluttering up, the name-space).

e.g. MyDate:

Code: Select all

' MyDate_Func.bas -- 2018-04-07, MrSwiss
'
' compile: -s console
'
' needed here, because: it is called, before the implementation!
Declare Function MyDate( ByVal nChr As UByte=0 ) ByRef As Const String

' ===== Start test/demo code =====
Print Date; Tab(16); "original american date"
Print MyDate(); Tab(16); "same delimtter, as above! (day/month swapped)"
Print MyDate( Asc("/") ); Tab(16); "new delimtter! (day/month swapped)"
Print MyDate( Asc(".") ); Tab(16); "new delimtter! (day/month swapped)"
Print
For i As UInteger = 1 To 3
    Print Mydate( Asc(".") )
Next
Print : Print "press a key, to exit ! ";

Sleep
' ===== End test/demo code =====

' ----- start implementation -----
Function MyDate( _      ' day/month swap (optional: 'user defined' delim.)
    ByVal nChr  As UByte=0 _    ' default = american delim.
    ) ByRef As Const String     ' reference to cDate (Static, read only!)
    Static As String   cDate, aDate ' local static var's
    ' check for: delimitter- or date-change (update not made otherwise!)
    If nChr <> Asc(Mid(cDate, 3, 1)) OrElse aDate <> Date Then
        aDate = Date            ' update aDate (american, for comparison!)
        If nChr > 0 Then        ' 'user defined' delim.? --> build cDate
            cDate = Mid(aDate, 4, 2) + Chr(nChr) + Left(aDate, 2) + _
                    Chr(nChr) + Right(aDate, 4)
        Else                    ' default, american delim. used
            nChr  = Asc(Mid(aDate, 3, 1))   ' get delim. --> build cDate
            cDate = Mid(aDate, 4, 2) + Chr(nChr) + Left(aDate, 2) + _
                    Chr(nChr) + Right(aDate, 4)
        End If
    End If
    ' return pre-made or newly created cDate-String (on change, only!)
    MyDate = cDate  ' the same as: Function = cDate / Return cDate
End Function
' ----- end implementation -----    ' ----- EOF -----
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Postby fxm » Apr 07, 2018 18:13

MrSwiss wrote:Alternative: use your own, customizable Function, which has the advantage, of
not needing, any #Include "whatever.bi" ... (cluttering up, the name-space).
.....

Maybe, but so much simpler for the same result:

Code: Select all

#include "vbcompat.bi"

Print Date
Print Format(Now, "dd-mm-yyyy")
Print Format(Now, "dd/mm/yyyy")
Print Format(Now, "dd.mm.yyyy")

Sleep
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Postby MrSwiss » Apr 07, 2018 18:53

Simpler? You must be joking:

Code: Select all

Print Format(Now, "dd-mm-yyyy")
' versus
Print MyDate( Asc("/") )  ' and the Asc("/") is optional, too
Print MyDate( )  ' default = american delimiter
Print MyDate( 46 )  ' second option: get the value straight from: ASCII-Table "."
Print MyDate( 47 )  ' second option: get the value straight from: ASCII-Table "/"
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Postby fxm » Apr 07, 2018 19:23

Let's ask ptitjoz's opinion!
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Postby MrSwiss » Apr 07, 2018 19:31

Nope, don't agree, for two reasons:
1) you are both french (sympathy bonus)
2) beginners don't usually have a, well-founded "opinion"
deltarho[1859]
Posts: 2083
Joined: Jan 02, 2017 0:34
Location: UK

Re: how to set the dates?

Postby deltarho[1859] » Apr 07, 2018 23:44

No comment re 1)

Re 2): A "well-founded" opinion is not a requirement - the question should be whether a beginner finds something simpler or not.

Using undefined constants is teaching a beginner bad tricks - should have stayed with Asc.
ptitjoz
Posts: 25
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

Re: how to set the dates?

Postby ptitjoz » Apr 08, 2018 6:49

Hello everyone.

Thank you for this discussion and the lines of code.
Being French does not change much. (except that I have trouble writing in English)
If I can give my opinion, I would say that we have to be pragmatic. For a beginner, if the code is simple, it's better.
For the internationalization of a program, I think we have to go through proven methods. But it was not (for now) the object of my request.

Regards

My code

Code: Select all

'Pgm testdate
'Mettre la date dans un autre format d'affichage
' Utisation d'une constante comme délimiteur
Const CDelim="/"
Dim NewDate as String
NewDate=Mid(date,4,2) & CDelim & Left(date,2) & CDelim & Right(date,4)
Print "Date origine ",date
Print "Date modif   ",NewDate
Sleep
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Postby MrSwiss » Apr 08, 2018 9:26

@ptitjoz,

your solution works, but it isn't updated, e.g. date change, while program is running.
Ease of use is OK however, specialties such as changes, *must* be considered, too.
From a speed point of view: updating is a *must*, but limited strictly by necessity.

This is what MyDate() is based on: no change = hand back *pre-processed*, process
only, if eiter "delimiter" or Date has changed, thus saving resources / incr. speed ...

@deltarho[],
since when, is the ASCII-Table, considered *undefined* ???
(Btw. they are "literals", not "constants" ...)
deltarho[1859]
Posts: 2083
Joined: Jan 02, 2017 0:34
Location: UK

Re: how to set the dates?

Postby deltarho[1859] » Apr 08, 2018 10:57

MrSwiss wrote:since when, is the ASCII-Table, considered *undefined* ???

When the values are taken out of context.
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: how to set the dates?

Postby jj2007 » Apr 08, 2018 11:03

MrSwiss wrote:your solution works, but it isn't updated, e.g. date change, while program is running.

Of course, ptitjoz could restart the program every 24 hours, but if that is not an acceptable workaround, here is the solution for updating (hit Ctrl C to exit):

Code: Select all

'Pgm testdate
'Mettre la date dans un autre format d'affichage
' Utisation d'une constante comme délimiteur
Const CDelim="/"
Dim NewDate as String
Dim NewTime as String
NewDate=Mid(date,4,2) & CDelim & Left(date,2) & CDelim & Right(date,4)

Print "Date origine ",date
Print "Date modif   ",NewDate

While 1
  NewTime=Mid(time,4,2) & CDelim & Left(time,2) & CDelim & Right(time,4)
  Print "Time origine ",time
  Print "Time modif   ",NewTime
  Sleep 1000
Wend
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Postby fxm » Apr 08, 2018 12:26

Could you correct your code for NewTime?
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: how to set the dates?

Postby jj2007 » Apr 08, 2018 12:38

Looks strange, I know. But the purpose was to demonstrate how to do the 24-hourly "update". And let's leave some work for the OP, too ;-)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest