AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Yesterday

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
PenP
Guest





PostPosted: Fri May 14, 2004 3:19 pm    Post subject: Yesterday Reply with quote

Hi all,

I'm stuck here with a problem.
I can't figger out how to create the day of yesterday in YYYYMMDD
Can somebody gif me a clue?

Grtz PenP
Back to top
beardboy



Joined: 02 Mar 2004
Posts: 444
Location: SLC, Utah

PostPosted: Fri May 14, 2004 7:44 pm    Post subject: Reply with quote

Here is an old CMD script I converted to AHK. Let me know if anyone finds bugs with it. I'm sure it could be written better.

Code:
; Program: Date Time
; Version: 00
; Last Modified: 2004.05.14
; Last Changes:

month = %a_mon%
day = %a_mday%
year = %a_year%
hour = %a_hour%
minute = %a_min%
second = %a_sec%

shortyear = %year%
shortyear -= 2000
if shortyear < 10
  shortyear = 0%shortyear%
syear = %shortyear%
yday = %day%
ymonth = %month%
yyear = %year%

if yday <> 1
{
  yday -= 1
}
else
{
  if ymonth = 01
  {
    ymonth = 12
    yday = 31
    yyear -= 1
  }
  else if ymonth = 02
  {
    ymonth = 01
    yday = 31
  }
  else if ymonth = 03
  {
    ymonth = 02
    yday = 28
    if yyear = 2004
    {
      yday = 29
    }
    else if yyear = 2008
    {
      yday = 29
    }
  }
  else if ymonth = 04
  {
    ymonth = 03
    yday = 31
  }
  else if ymonth = 05
  {
    ymonth = 04
    yday = 30
  }
  else if ymonth = 06
  {
    ymonth = 05
    yday = 31
  }
  else if ymonth = 07
  {
    ymonth = 06
    yday = 30
  }
  else if ymonth = 08
  {
    ymonth = 07
    yday = 31
  }
  else if ymonth = 09
  {
    ymonth = 08
    yday = 31
  }
  else if ymonth = 10
  {
    ymonth = 09
    yday = 30
  }
  else if ymonth = 11
  {
    ymonth = 10
    yday = 31
  }
  else if ymonth = 12
  {
    ymonth = 11
    yday = 30
  }
}

if yday < 10
  yday = 0%yday%
if yyear > 1999
{
  ysyear = %yyear%
  ysyear -= 2000
}
if ysyear < 10
  ysyear = 0%ysyear%

MsgBox, Today:`t`t%month%.%day%.%year% (%syear%)`nYesterday:`t%ymonth%.%yday%.%yyear% (%ysyear%)`n`nTime:`t`t%hour%:%minute%:%second%

thanks,
beardboy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Guest






PostPosted: Fri May 14, 2004 10:50 pm    Post subject: Reply with quote

Thanks Beardboy,

This is exactly what i was looking for but could not figure out myself. Very Happy
I dont know if the script could be written better, i only changed the script for the 1st of march. The script as it is right now, will work until 2011.
This is what i changed about it.

Code:
  else if ymonth = 02
  {
    ymonth = 01
    yday = 31
  }
  else if ymonth = 03
  {
    ymonth = 02
    yday = 28
    leapyear = %a_year%
    envdiv, leapyear, 4
    envmult, leapyear, 4
    envsub, leapyear, %a_year%
  ifnotequal leapyear, 0
  {
    yday = 29
  }
  }
  else if ymonth = 04


I think the script is not the most efficient but it works fine this way.

Grtz PenP
Back to top
PenP



Joined: 14 May 2004
Posts: 10
Location: The Netherlands

PostPosted: Fri May 14, 2004 11:41 pm    Post subject: Reply with quote

Ifnotequal must be "Ifequal" Confused

This is the script as i use it right now:

Code:
yday = %a_mday%
ymonth = %a_mon%
yyear = %a_year%

if yday <> 1
{
  yday -= 1
}
else
{
  if ymonth = 01
  {
    ymonth = 12
    yday = 31
    yyear -= 1
  }
  else if ymonth = 02
  {
    ymonth = 01
    yday = 31
  }
  else if ymonth = 03
  {
    ymonth = 02
    yday = 28
    leapyear = %a_year%
    envdiv, leapyear, 4
    envmult, leapyear, 4
    envsub, leapyear, %a_year%
  ifequal leapyear, 0
  {
    yday = 29
  }
  }
  else if ymonth = 04
  {
    ymonth = 03
    yday = 31
  }
  else if ymonth = 05
  {
    ymonth = 04
    yday = 30
  }
  else if ymonth = 06
  {
    ymonth = 05
    yday = 31
  }
  else if ymonth = 07
  {
    ymonth = 06
    yday = 30
  }
  else if ymonth = 08
  {
    ymonth = 07
    yday = 31
  }
  else if ymonth = 09
  {
    ymonth = 08
    yday = 31
  }
  else if ymonth = 10
  {
    ymonth = 09
    yday = 30
  }
  else if ymonth = 11
  {
    ymonth = 10
    yday = 31
  }
  else if ymonth = 12
  {
    ymonth = 11
    yday = 30
  }
}


MsgBox, Today:`t`t%a_mday%-%a_mon%-%a_year%`nYesterday:`t%yday%-%ymonth%-%yyear%


Thanks again... Very Happy
Back to top
View user's profile Send private message
beardboy



Joined: 02 Mar 2004
Posts: 444
Location: SLC, Utah

PostPosted: Fri May 14, 2004 11:50 pm    Post subject: Reply with quote

Anytime.

thanks,
beardboy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10474

PostPosted: Sat May 15, 2004 12:36 am    Post subject: Reply with quote

AHK has built-in date-time math. See EnvAdd and EnvSub in the help file for details.

The current time can be put into a variable in either of these ways:
EmptyVar += 0, days ; Must be empty for this to work.
AnyVar = %a_year%%a_mon%%a_mday%%a_hour%%a_min%%a_sec%

Once you put the current time into a variable, simply add "-1 days" to it to get "yesterday":
CurrentTime += -1, days

Then, if you don't want the time component, chop it off:
StringLeft, CurrentTime, CurrentTime, 8 ; Leaves only YYYYMMDD

Also, you can use EnvSub to find out how many days, hours, minutes, or seconds are between two timestamps.
Back to top
View user's profile Send private message Send e-mail
beardboy



Joined: 02 Mar 2004
Posts: 444
Location: SLC, Utah

PostPosted: Sat May 15, 2004 12:42 am    Post subject: Reply with quote

I knew AHK could do date / time math, but for some reason I didn't think it would handle the switch to the previous month. Good to know. Wink And a lot easier than my old CMD script converted to AHK.

thanks,
beardboy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10474

PostPosted: Sat May 15, 2004 1:27 am    Post subject: Reply with quote

I should probably add it to the FAQ since dates and times are frequently worked with, and yet the secret of how to manipulate them is buried in a fairly obscure place in the documentation.
Back to top
View user's profile Send private message Send e-mail
PenP



Joined: 14 May 2004
Posts: 10
Location: The Netherlands

PostPosted: Sat May 15, 2004 11:46 am    Post subject: Reply with quote

Thanks, this is much more efficient and it works great!
This is wat I made off it.

Code:
EmptyVar += 0, days

CurrentTime = %a_year%%a_mon%%a_mday%%a_hour%%a_min%%a_sec%

CurrentTime += -1, days

StringLeft, y_year, CurrentTime, 4
StringMid, y_mon, CurrentTime, 5, 2
StringMid, y_day, CurrentTime, 7, 2

MSgBox, Today: `t`t%a_year%-%a_mon%-%a_mday% `nYesterday: `t%y_year%-%y_mon%-%y_day%


Grtz PenP
Back to top
View user's profile Send private message
Hotfoot
Guest





PostPosted: Sat Apr 30, 2005 7:11 pm    Post subject: Reply with quote

I was wondering if the 'yesterday date' could be formatted with the abbreviated month name (e.g: Apr 30 05). I have experimented with many different versions but could not get anything to work with the MMM format. I developed a workaround solution but is there an easier way?

(My code is not going to work on the 1st of a month -- If today's date is May 01 05, then it will type out yesterday's date as May 30 05 when it should be Apr 30 05.)

Code:

#3::           ; Insert yesterday's date
FormatTime, TimeString, ,MMM dd yy:
clipboard = %TimeString%
Send, ^v
Send,{Left 4}
Send,{Backspace 2}

EmptyVar += 0, days

CurrentTime = %a_year%%a_mon%%a_mday%

CurrentTime += -1, days

StringMid, y_day, CurrentTime, 7, 2

clipboard = %y_day%
Send, ^v
Send,{right 4}
return
Back to top
beardboy



Joined: 02 Mar 2004
Posts: 444
Location: SLC, Utah

PostPosted: Sat Apr 30, 2005 7:53 pm    Post subject: Reply with quote

Is this what you are looking for?
Code:
today = %a_now%
today += -1, days
FormatTime, today, %today%, MMM dd yy
MsgBox, %today%

thanks,
beardboy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Hotfoot
Guest





PostPosted: Sat Apr 30, 2005 8:24 pm    Post subject: Reply with quote

Thank you. Thank you. I guess trying to follow the previous post's method made me experiment with different things with undesireable results. I'm still learning. Confused
Back to top
garry



Joined: 19 Apr 2005
Posts: 1157
Location: switzerland

PostPosted: Sat Apr 30, 2005 10:45 pm    Post subject: Reply with quote

Code:
EnvAdd, B,-1,days
stringleft,A,B,8
Msgbox,%A%
Back to top
View user's profile Send private message
Grendel



Joined: 18 Nov 2005
Posts: 25
Location: Germany

PostPosted: Sun Jan 15, 2006 2:20 pm    Post subject: Reply with quote

or as function...

Code:

MsgBox % Yesterday()

Yesterday()
{
  y:=a_now
  y+=-1,days
  FormatTime,y,%y%,yyyyMMdd  ; <--- format your output string here
  Return,y
}

_________________
greets Grendel
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group