How to Get Date?

Hi team!

I want to display the Actual Date, in this format: “June 25 2015”

I have this code:
Dim d As xojo.Core.Date = Xojo.Core.Date.Now
msgbox d.ToText

But I’got this: 205/06/2015 03:48

Is this possible to do it?

match.com?

(Sorry, couldn’t resist. Actual response coming up.)

The Date.ToText method will return a SQLDate. If you want to convert that to its English (or other language) equivalent, you will have to do it yourself by parsing the Month, Day, and Year yourself.

There may also be a plugin or system call that does this for you, but nothing native to Xojo.

On a Mac, you can also use Applescript to substitute it for you. For example, this code through a shell will return “June”:

osascript -e '(month of date "6/1/2015") as text'

[quote=196894:@Kem Tekinay]match.com?

(Sorry, couldn’t resist. Actual response coming up.)[/quote]
LOL!

That was my first thought too when I saw the thread title!

How do I get to see the photos? :slight_smile: :slight_smile:

It is really bad when you have to troll technical forums to find a date…

:slight_smile:

[quote=196892:@Gerardo García]Hi team!

I want to display the Actual Date, in this format: “June 25 2015”

I have this code:
Dim d As xojo.Core.Date = Xojo.Core.Date.Now
msgbox d.ToText

But I’got this: 205/06/2015 03:48[/quote]

Dim d As xojo.Core.Date = Xojo.Core.Date.Now dim months() as string = split("January,February,March,April,May,June,July,August,September,October,November,December",",") dim datenotime() as string = split(d.ToText," ") dim dateelements() as string = Split(datenotime(0),"-") Msgbox months(val(dateelements(1))-1)+" "+ dateelements(2)+" "+dateelements(0)

On a US System, date appears as 2015-06-25 23:21:08 so I split datenotime(0) with “-.” You may need to use “/” since that is how you describe it, it maybe the result of your locale.

[quote=196894:@Kem Tekinay]match.com?

(Sorry, couldn’t resist. Actual response coming up.)[/quote]
LOL Well Done!!

[quote=196923:@Michel Bujardet] Dim d As xojo.Core.Date = Xojo.Core.Date.Now
dim months() as string = split(“January,February,March,April,May,June,July,August,September,October,November,December”,",")
dim datenotime() as string = split(d.ToText," “)
dim dateelements() as string = Split(datenotime(0),”-")
Msgbox months(val(dateelements(1))-1)+" “+ dateelements(2)+” "+dateelements(0)[/quote]
Its perfect!

[quote=196892:@Gerardo García]Hi team!

I want to display the Actual Date, in this format: “June 25 2015”

I have this code:
Dim d As xojo.Core.Date = Xojo.Core.Date.Now
msgbox d.ToText

But I’got this: 205/06/2015 03:48[/quote]
Try this:
msgbox d.ToText(new xojo.Core.Locale(“en_US”),Xojo.Core.Date.FormatStyles.Long,xojo.Core.Date.FormatStyles.None).Replace(",","")

or with using:

[code] using xojo.Core

dim d as date=date.now
MsgBox d.ToText(new Locale(“en_US”),Date.FormatStyles.Long,Date.FormatStyles.None).Replace(",","")[/code]

I haven’t played with the new framework stuff yet, so this code is pretty much for the old Date class (am waiting for 64-bit before I purchase a new license)… This code is for Cocoa and will allow you to use strings to format the date how you want it… I have code for Windows and Linux that I need to convert and test…

[code]Function ToString(Extends aDate As Date, format As String) As String
DIM returnValue As String

#if TargetCocoa
Declare Function alloc Lib “Cocoa” Selector “alloc” (classRef As Ptr) As Ptr
Declare Function currentLocale Lib “Cocoa” Selector “currentLocale” (classRef As Ptr) As Ptr
Declare Function dateWithTimeIntervalSince1970 Lib “Cocoa” Selector “dateWithTimeIntervalSince1970:” (classRef As Ptr, seconds As Double) As Ptr
Declare Function init Lib “Cocoa” Selector “init” (instanceRef As Ptr) As Ptr
Declare Function NSClassFromString Lib “Cocoa” (className As CFStringRef) As Ptr
Declare Function stringFromDate Lib “Cocoa” Selector “stringFromDate:” (aNSDateFormatter As Ptr, aNSDate As Ptr) As CFStringRef
Declare Sub setDateFormat Lib “Cocoa” Selector “setDateFormat:” (aNSDateFormatter As Ptr, format As CFStringRef)
Declare Sub setLocale Lib “Cocoa” Selector “setLocale:” (aNSDateFormatter As Ptr, locale As Ptr)

Static NSDateClass As Ptr = NSClassFromString("NSDate")
Static NSDateFormatterClass As Ptr = NSClassFromString("NSDateFormatter")
Static NSLocaleClass As Ptr = NSClassFromString("NSLocale")

if (InStr(format, "z") > 0) OR (InStr(format, "O") > 0) OR (InStr(format, "x") > 0) then  // modify the time for the time zone
  aDate.Hour = aDate.Hour - aDate.GMTOffset
end if

DIM aNSDateFormatter As Ptr = init(alloc(NSDateFormatterClass))
setLocale aNSDateFormatter, currentLocale(NSLocaleClass)
setDateFormat aNSDateFormatter, format

DIM since1970 As NEW Date
since1970.SQLDateTime = "1970-01-01 00:00:00"

DIM seconds As Double
seconds = aDate.TotalSeconds - since1970.TotalSeconds - (3600 * aDate.GMTOffset)

returnValue = stringFromDate(aNSDateFormatter, dateWithTimeIntervalSince1970(NSDateClass, seconds))

if (InStr(format, "z") > 0) OR (InStr(format, "O") > 0) OR (InStr(format, "x") > 0) then  // restore the time
  aDate.Hour = aDate.Hour + aDate.GMTOffset
end if

#endif

Return returnValue

#Pragma Unused aDate
#Pragma Unused format
End Function
[/code]

Here are a bunch of constants that I use for different formats…

Const WeekdayNameFormat As String = "EEEE" Const AbbreviatedMonthNameFormat As String = "MMM" Const AbbreviatedWeekdayNameFormat As String = "EEE" Const CookieFormat As String = "EEEE, dd-MMM-yy kk:mm:ss zzz" Const FullDateFormat As String = "EEEE, MMMM d, yyyy" Const FullTimeFormat As String = "h:mm:ss a z" Const ISO8601DateFormat As String = "yyyy-MM-dd" Const ISO8601Format As String = "yyyy-MM-dd'T'HH:mm:ssZZZ" Const MediumDateFormat As String = "MMM d, yyyy" Const MediumTimeFormat As String = "h:mm:ss a" Const MonthNameFormat As String = "MMMM" Const RFC2822Format As String = "EEE, d MMM yyyy kk:mm:ss ZZZ" Const RFC3339Format As String = "yyyy-MM-dd'T'kk:mm:sszzz"

Just place the method and the constants in to a module to use…

Example:

DIM d As NEW Date MsgBox d.ToString(ISO8601Format)

Just a note…

dim months() as string = split("January,February,March,April,May,June,July,August,September,October,November,December",",")

should be replaced by

  dim months() as String = Array("January","February","March","April","May","June","July","August","September","October","November","December")

Thomas,

They both give you the same result, but the “split” version is easier to write while the “array” version is probably faster. “Dim” should be replaced by “static” in either case so it only has to do it once.

Shao, one minor suggestion:

#if TargetCocoa
  // Your code
#else
  #pragma unused aDate
  #pragma unused format
#endif

return returnValue

yeah, I was doing that before, but when i change the name of the parameters, it throws an error on the other platforms… “unused” doesn’t mean they’re not being used…

http://documentation.xojo.com/index.php/Pragma

You gotta PEEK before you can POKE.