Formatting SQLDate

I’m trying to generate the current date in an ios app, and I just want to use the SQLDate Format.

In desktop apps you just use this code:

//Sets Date to current Date Dim d as Date d= New Date txtDate.Text= d.SQLDate

In iOS the code is:

Dim d As Xojo.Core.Date = Xojo.Core.Date.Now Dim SQLDate As Text = d.ToText ' 2017-10-31 09:38:24 txtDate.Text= d.ToText

It adds the time with the date. I just want the date, and not the time. Does anyone know how to get rid of the time from the code and just have a regular SQLDate?

use Left method ? or Nthfield(d.totext, " ",1) ?

It’s for iOS, using the new framework i’d just parse it into a Xojo.Core.Date and make a method that returns your specified Text

Edito forgot that the ToText function on Xojo.Core.Date accepts Format types. See the docs here:
http://developer.xojo.com/xojo-core-date$ToText

Use any format style for The date parameter, use formatstyle.none to the time parameter. You may set locale to nil or specified

I knew it was too obvious … :wink:

This code gets rid of the time but the format is not SQLDate.

Using Xojo.Core Dim d As Date = Date.Now Dim t As Text = d.ToText(Locale.Current, Date.FormatStyles.Short, Date.FormatStyles.None) txtDate.Text= t

I don’t even see any way for formatting SQLDate. There is just SQLDateTime

Dim d As Xojo.Core.Date = Xojo.Core.Date.Now Dim SQLDateTime As Text = d.ToText ' 2017-10-31 09:38:24

I just decided to make my own. This will work. Thank for your help. I appreciate you takin the time to help.

[code]Dim d As Xojo.Core.Date = Xojo.Core.Date.Now
Dim year As Integer = d.Year
txtYear2.Text=d.Year.ToText

Dim month As Integer = d.Month
txtMonth2.Text=d.Month.ToText

Dim day As Integer = d.Day
txtDay2.Text=d.Day.ToText

txtDate.Text=txtYear2.Text + “-” + txtMonth2.Text + “-” + txtDay2.Text[/code]

[quote=367822:@James Redway]Dim d As Xojo.Core.Date = Xojo.Core.Date.Now
Dim year As Integer = d.Year
txtYear2.Text=d.Year.ToText

Dim month As Integer = d.Month
txtMonth2.Text=d.Month.ToText

Dim day As Integer = d.Day
txtDay2.Text=d.Day.ToText

txtDate.Text=txtYear2.Text + “-” + txtMonth2.Text + “-” + txtDay2.Text[/quote]
Are you sure?

Today’s SQLDate should be 2018-01-06 and I think your code will report 2018-1-6

Can’t you use this in iOS?

Dim d As Xojo.Core.Date = Xojo.Core.Date.Now Dim SQLDate As Text = d.ToText.Left(10) ' 2018-01-06

Sorry if I’m wrong, I’m new and learning.

Yes I saw that so I changed the code to fix that. Lots of code for something that should be a single line, but any port in a storm.

[code]
Dim d As Xojo.Core.Date = Xojo.Core.Date.Now

Dim year As Integer = d.Year
txtYear2.Text=d.Year.ToText

Dim month As Integer = d.Month
txtMonth2.Text=d.Month.ToText

Dim Day As Integer = d.Day
txtDay2.Text=d.Day.ToText

If txtMonth2.Text=“1” Then
txtMonth2.Text=“01”

ElseIf txtMonth2.Text=“2” Then
txtMonth2.Text=“02”

ElseIf txtMonth2.Text=“3” Then
txtMonth2.Text=“03”

ElseIf txtMonth2.Text=“4” Then
txtMonth2.Text=“04”

ElseIf txtMonth2.Text=“5” Then
txtMonth2.Text=“05”

ElseIf txtMonth2.Text=“6” Then
txtMonth2.Text=“06”

ElseIf txtMonth2.Text=“7” Then
txtMonth2.Text=“07”

ElseIf txtMonth2.Text=“8” Then
txtMonth2.Text=“08”

ElseIf txtMonth2.Text=“9” Then
txtMonth2.Text=“09”

End If

If txtDay2.Text=“1” Then
txtDay2.Text=“01”

ElseIf txtDay2.Text=“2” Then
txtDay2.Text=“02”

ElseIf txtDay2.Text=“3” Then
txtDay2.Text=“03”

ElseIf txtDay2.Text=“4” Then
txtDay2.Text=“04”

ElseIf txtDay2.Text=“5” Then
txtDay2.Text=“05”

ElseIf txtDay2.Text=“6” Then
txtDay2.Text=“06”

ElseIf txtDay2.Text=“7” Then
txtDay2.Text=“07”

ElseIf txtDay2.Text=“8” Then
txtDay2.Text=“08”

ElseIf txtDay2.Text=“9” Then
txtDay2.Text=“09”

End If

txtDate.Text=txtYear2.Text + “-” + txtMonth2.Text + “-” + txtDay2.Text

//Get Day Of The Week
Dim dayWeek As Integer = Xojo.Core.Date.Now.DayOfWeek
txtDay.Text=dayWeek.ToText

If dayWeek=1 then
txtDay.Text=“Sunday”

ElseIf dayWeek=2 Then
txtDay.Text=“Monday”

ElseIf dayWeek=3 Then
txtDay.Text=“Tuesday”

ElseIf dayWeek=4 Then
txtDay.Text=“Wednesday”

ElseIf dayWeek=5 Then
txtDay.Text=“Thursday”

ElseIf dayWeek=6 Then
txtDay.Text=“Friday”

ElseIf dayWeek=7 Then
txtDay.Text=“Saturday”

End If[/code]

I needed this format to sync with my Windows/macOS app that uses this format.

It might not be a single line but Alberto gave you the sqlDate in a line.
You can also parse the full date to get the day of week in a single line.

Using Xojo.Core
Dim d As Date = Date.Now
Dim SQLDate As Text = d.ToText.Left(10)  ' 2018-01-06
Dim dayWeek As Text = NthField(d.ToText(Locale.Current, Date.FormatStyles.Full, Date.FormatStyles.None),",",1).ToText

Sorry, I still don’t understand why you do this. Can’t you do this?

[code]Dim d As Xojo.Core.Date = Xojo.Core.Date.Now
Dim year As Integer = d.Year
Dim month As Integer = d.Month
Dim Day As Integer = d.Day

txtYear2.Text = d.ToText.Left(4)
txtMonth2.Text = d.ToText.Mid(5,2)
txtDay2.Text = d.ToText.Mid(8,2)

txtDate.Text = d.ToText.Left(10)[/code]

I guess you need year, month, day, and have labels/controls txtYear2, txtMonth2, txtDay2 and txtDate

Hey Jim, good information. Thanks to you I learned about NthField and a little bit more about Xojo.Core.

Because I didn’t put “Using Xojo.Core”, to make it work, I had to change your code to:

Dim d As Xojo.Core.Date = Xojo.Core.Date.Now Dim SQLDate As Text = d.ToText.Left(10) ' 2018-01-06 Dim dayWeek As Text = NthField(d.ToText(Xojo.Core.Locale.Current, Xojo.Core.Date.FormatStyles.Full, Xojo.Core.Date.FormatStyles.None),",",1).ToText

So is better to start with ‘Using Xojo.Core’

Edit: I can’t make NthField work with iOS project. I think is for String only and can’t be used with iOS. I used split to make it work:

Using Xojo.Core Dim d As Date = Date.Now Dim SQLDate As Text = d.ToText.Left(10) ' 2018-01-06 Dim dayWeekArray() As Text = d.ToText(Locale.Current, Date.FormatStyles.Full, Date.FormatStyles.None).Split(",") Dim dayWeek As Text = dayWeekArray(0) ' Saturday

[quote=367851:@Jim Shaffer]It might not be a single line but Alberto gave you the sqlDate in a line.
You can also parse the full date to get the day of week in a single line.[/quote]

The SQDate still had the time and I just did not know how to parse that Jim. Thank you for your help. :slight_smile:

[quote=367856:@Alberto De Poo]Sorry, I still don’t understand why you do this. Can’t you do this?

Dim d As Xojo.Core.Date = Xojo.Core.Date.Now
Dim year As Integer = d.Year
Dim month As Integer = d.Month
Dim Day As Integer = d.Day

txtYear2.Text = d.ToText.Left(4)
txtMonth2.Text = d.ToText.Mid(5,2)
txtDay2.Text = d.ToText.Mid(8,2)

txtDate.Text = d.ToText.Left(10)[/quote]

Again. I just did not know how to do that.
That works great and a lot less code. Thank you very much for your help. I really appreciate it. :slight_smile:

I’m glad I was able to help.

I learn a lot from the forum and testing the code I see here.

I usually use this code to get current SQLDate:

Dim SQLDate as Text SQLDate = xojo.Core.date.now.toText.left(10)

Thank you Jrmie,

I just did a test and that code could even be in a single line, maybe that’s what James want:

Dim SQLDate As Text = Xojo.Core.Date.Now.ToText.Left(10)