Get Dates in YYYYMMDD Format

Hi All,

How can I get the Dates from today and yesterday in YYYYMMDD Format?

Quick & Dirty: theDateObject.SQLDate.ReplaceAll("-", “”)

In the new framework using Xojo.Core.Date, the toText() method (by default) returns dates in the following format:

YYYY-MM-DD HH:MM:SS

http://developer.xojo.com/xojo-core-date$ToText

dim date as new date date.day = date.day + 1 'date.day = date.day -1 dim formated_date as string = str(date.year)+str(date.month)+str(date.day) msgbox(formated_date)

Thank you :wink:

[quote=327989:@nicolscanessa] dim date as new date
date.day = date.day + 1 'date.day = date.day -1
dim formated_date as string = str(date.year)+str(date.month)+str(date.day)
msgbox(formated_date)[/quote]

Thank you for the response.
I tried it, but what I get is YYYYMDD instead of YYYYMMDD

[quote=327994:@Ricardo Grebien]Thank you for the response.
I tried it, but what I get is YYYYMDD instead of YYYYMMDD[/quote]
Use:

dim date as new date date.day = date.day + 1 'date.day = date.day -1 dim formated_date as string = str(date.year, "0000")+str(date.month,"00")+str(date.day,"00") msgbox(formated_date)
instead.

dim theDate as new date dim formated_date_today as string = theDateObject.SQLDate.ReplaceAll("-", "") theDate.totalseconds= theDate.totalseconds - 86400 // Current Date - 1 Day dim formated_date_yesterday as string = theDateObject.SQLDate.ReplaceAll("-", "")

SQL Date Format is always YYYY-MM-DD. So you WILL endup with 20170427 for today for example.

BTW

str(date.month) // This will remove the leading 0 before the month is converted to a String

Edit: I think Date.Month has no leading 0 :wink:

You know about removing the hyphens from
YYYY-MM-DD HH:MM:SS

I assume you know how to use left() to get the first n characters?

[quote=328000:@Simon Berridge]dim date as new date
date.day = date.day + 1 'date.day = date.day -1
dim formated_date as string = str(date.year, “0000”)+str(date.month,“00”)+str(date.day,“00”)
msgbox(formated_date)[/quote]

Thank you, it really helped me :slight_smile:

Date.Day gives the day of the year. I never tested what happens if you do the Date.Day - 1 on the 1st day of the Year. :wink:
Because of this i always use Date.TotalSeconds (i know there can be such issues too but it’s far less relevant for my projects) :slight_smile:

What do you mean Sasha ?

[quote]Day As Integer (read-only)
The day of the month as a number.[/quote]

AFAIK there is no such thing as dayoftheyear. Although you could calculate it from date.Totalseconds.

[quote=328153:@Michel Bujardet]What do you mean Sasha ?

AFAIK there is no such thing as dayoftheyear. Although you could calculate it from date.Totalseconds.[/quote]
Sorry, you are right. :slight_smile:
But there is indeed a DayOfYear.
Will the Year also go -1 if you do a Day -1 on the 1st of January?

PS: I am not a my Dev. Desk currently and can’t perform tests by myself. thank you. :slight_smile:

@Richardo: If you use the new Framework, you may like to take a look at DateInterval :slight_smile:

[quote=328156:@Sascha S]But there is indeed a DayOfYear .
Will the Year also go -1 if you do a Day -1 on the 1st of January?[/quote]

Indeed, both Date has that.

If it works like anything else in Xojo date, you should see DayOfYear go to 365 and Year decrease by one.