-6

I'm using VBS at my workplace and I would like to convert today's date to an integer like so:

01/02/2018 → 180201

Any ideas? I'm lost.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Gal Gueta
  • 9
  • 1
  • 1
  • 3
    What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question. – Jeff Zeitlin Feb 01 '18 at 18:09
  • An integer can hold +/- 32K. So 80K is too big. Use a Long +/- 2 Gig. Use `Mid` to chop the numbers out. Build a string. Then `CLng` the string. – ACatInLove Feb 01 '18 at 21:38
  • @ACatInLove did you not notice the answers already posted? – user692942 Feb 01 '18 at 23:52

3 Answers3

1

You need a Year(),Month() and Day() or other Date Functions in combination with Right(), to ISO date "YYYYMMDD":

IsoDate=CStr(Year(Date)) & Right("0" & CStr(Month(Date)),2) & Right("0" & CStr(Day(Date)),2)

If you want a "YYMMDD" date format:

IsoDate= Right(CStr(Year(Date)),2) & Right("0" & CStr(Month(Date)),2) & Right("0" & CStr(Day(Date)),2)

Then you can convert the date to a number with CInt() or other Data Type Conversion Functions

IntIsoDate = Cint(IsoDate)
user692942
  • 16,398
  • 7
  • 76
  • 175
  • There is a simplier way `TDay = Right(“00”, Day(Date()), 2)` to pad date parts less then two digits. Plus there is no need for `CStr()` it’s just noise. – user692942 Feb 01 '18 at 18:32
  • Why you linking .Net documentation? – user692942 Feb 01 '18 at 18:42
  • Typo, was meant to be `TDay = Right("00" & Day(Date()), 2)`. – user692942 Feb 01 '18 at 18:44
  • @Lankymart No need for two zeros, one is enough because you'll always have at least one digit coming from the `Day()` function. – Racil Hilan Feb 01 '18 at 18:46
  • @RacilHilan Each to their own, some use 2, some use 1. – user692942 Feb 01 '18 at 18:48
  • @Lankymart Thkns for the code correction, the sintax in the documentation is the same in VBScript and .Net – Ricardo Galain Feb 01 '18 at 18:51
  • @Lankymart Well, it's not a matter of opinion or liking, it's a matter of efficiency and code logic. Otherwise, somebody can put 100 just because they like it. Strings consume memory and CPU cycles. For a one off use, not a big deal, but you can see a difference in a loop or heavy use. – Racil Hilan Feb 01 '18 at 18:53
  • @RacilHilan disagree. But like you say it's a matter of opinion. – user692942 Feb 01 '18 at 18:53
  • @Lankymart I'm glad we disagree :). I said it's **NOT** a matter of opinion or liking :). Oh, but then this is my opinion, like Sheldon's mom said to him in The Big Bang Theory :). – Racil Hilan Feb 01 '18 at 18:59
  • @RacilHilan it makes little to no difference in VBScript, 20 minutes ago you were trying to convince me that Excel VBA and VBScript are the same. I've been coding VBScript for years and that method has never given me any trouble even on thousands of iterations. We are literally splitting hairs to be honest, i'll leave it there. – user692942 Feb 01 '18 at 19:03
  • @Lankymart It won't give you any trouble, who said it would? It will just take a bit longer, that's all. If you don't care, then put as many zeros as you want. If you do, then one zero is enough and the most efficient. And what does this have to do with the language? It is the same in all languages. Strings use memory and CPU cycles in every language, so the less the better. – Racil Hilan Feb 01 '18 at 19:06
  • @RacilHilan honestly, personally I'd prefer if a null value is passed that I still get a value padding to 2 digits rather then a 1 digit which breaks the formatting I'm after in this scenario. – user692942 Feb 01 '18 at 19:11
  • @Lankymart OK, now this can be a valid reason, if you care to return the same format even when you pass them an invalid date. Now we agree on something :). I personally prefer to follow the saying "garbage in, garbage out" :). It makes it easier to recognize bugs rather than hiding them. But I agree that there are cases were we really don't care and may just want it to work and ignore errors or invalid entries. So I guess, now it is not a matter of opinion, but a matter of usage. You want it efficient and show errors, use 1 zero; you want it to ignore errors, use 2 zeros. Nice! – Racil Hilan Feb 01 '18 at 19:36
0

You can use the Now to get the current date & time, and the Format() function below to format in the way you want:

Function Format(dt)
    dim d, m, y
    d = Right("0" & Day(dt), 2)
    m = Right("0" & Month(dt), 2)
    y = Right(Year(dt), 2)
    Format = y & m & d
End Function

This gives you the value as a String. You wanted it as an Integer, but it is too big for an Integer and if you try to convert it, it will give you an OverFlow exception. You can convert it to Long if you want, using the CLng() function:

CLng(Format(Now))
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • You've wrapped it in a function, but nothing that hasn't [been mentioned before](https://stackoverflow.com/a/22575530/692942). – user692942 Feb 01 '18 at 18:47
  • Hmm, I didn't see that answer before, but yes almost everything is mentioned there. Well, except my explanation under the function, which makes it a different answer. – Racil Hilan Feb 01 '18 at 18:49
  • You mean the one line that converts a string to a long, ok. – user692942 Feb 01 '18 at 18:50
  • Not only the one line, but the paragraph above it. You cannot convert that value to an integer. – Racil Hilan Feb 01 '18 at 18:54
0

If "solid number" is taken seriously, the question can't be a duplicate of a question that asks for format==convert-to-a-string a date.

Then correct answers either

Demo:

Option Explicit

Function Date2Long(dt)
  ' Add checks that make sense given your specs. Delete if you know what you are doing
  If VarType(dt) <> vbDate Then Err.Raise 4711, "need a Date", "Date2Long"
  If Year(dt) < 2001 Then Err.Raise 4712, dt & "? - need a Date >= 2001", "Date2Long"
  ' ...
  Date2Long = 10000 * (Year(dt) - 2000) + 100 * Month(dt) + Day(dt)
End Function

Dim dt, n, d 
For Each dt In Array(#2/1/2018#, #2/2/2018#, #2/3/2018#, #1/1/2001#, #11/11/2011#, #12/31/2099#, #12/31/2100#)
    n = Date2Long(dt)
    d = CDbl(dt)
    WScript.Echo TypeName(dt), dt, TypeName(n), n, TypeName(d), d, CStr(d = Fix(d)) 
Next

'WScript.Echo Date2Long("pipapo")
WScript.Echo Date2Long(#12/31/2000#)

output (german locale):

cscript 48569157.vbs
Date 01.02.2018 Long 180201 Double 43132 Wahr
Date 02.02.2018 Long 180202 Double 43133 Wahr
Date 03.02.2018 Long 180203 Double 43134 Wahr
Date 01.01.2001 Integer 10101 Double 36892 Wahr
Date 11.11.2011 Long 111111 Double 40858 Wahr
Date 31.12.2099 Long 991231 Double 73050 Wahr
Date 31.12.2100 Long 1001231 Double 73415 Wahr
E:\work\proj\soa\48569157\vbs\48569157.vbs(6, 27) 31.12.2000? - need a Date >= 2001: Date2Long
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • 1
    Why reopen this? The OP hasn't shown any attempt, theres no [mcve] and it was clearly a duplicate, the fact they talk about it being an integer is irrelevant. – user692942 Feb 01 '18 at 23:46