how to set the dates?

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

how to set the dates?

Post by ptitjoz »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Post by fxm »

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: 32
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

Re: how to set the dates?

Post by ptitjoz »

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

Re: how to set the dates?

Post by MrSwiss »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Post by fxm »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Post by MrSwiss »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Post by fxm »

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

Re: how to set the dates?

Post by MrSwiss »

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: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: how to set the dates?

Post by deltarho[1859] »

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: 32
Joined: Jun 24, 2017 8:10
Location: France, centre
Contact:

Re: how to set the dates?

Post by ptitjoz »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how to set the dates?

Post by MrSwiss »

@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: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: how to set the dates?

Post by deltarho[1859] »

MrSwiss wrote:since when, is the ASCII-Table, considered *undefined* ???
When the values are taken out of context.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: how to set the dates?

Post by jj2007 »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how to set the dates?

Post by fxm »

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

Re: how to set the dates?

Post by jj2007 »

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 ;-)
Post Reply