Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - How to reverse DatePart("y",Date) ?

Reply
 
Old 07-06-2009   #1 (permalink)
Brad


 
 

How to reverse DatePart("y",Date) ?

DatePart("y",Date)

This would give an integer day of the year representing today but how
do you achieve the opposite?. I am supplied with the int day or year,
and need to convert that into a date.... any ideas?

My System SpecsSystem Spec
Old 07-06-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: How to reverse DatePart("y",Date) ?


"Brad" <brad@xxxxxx> wrote in message
news:9c79020e-4d98-44aa-aff2-bc0a8b853acc@xxxxxx
Quote:

> DatePart("y",Date)
>
> This would give an integer day of the year representing today but how
> do you achieve the opposite?. I am supplied with the int day or year,
> and need to convert that into a date.... any ideas?
Have a look at the CDate function. The help file script56.chm, which you can
download from the Microsoft site, has some detailed examples.


My System SpecsSystem Spec
Old 07-06-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: How to reverse DatePart("y",Date) ?


"Brad" <brad@xxxxxx> wrote in message
news:9c79020e-4d98-44aa-aff2-bc0a8b853acc@xxxxxx
Quote:

> DatePart("y",Date)
>
> This would give an integer day of the year representing today but how
> do you achieve the opposite?. I am supplied with the int day or year,
> and need to convert that into a date.... any ideas?
Perhaps:

intDay = DatePart("y", Date())
dtmToday = DateAdd("d", #12/31/2008#, intDay)

Or:

intDay = DatePart("y", Date())
intYear = Year(Date())
dtmStart = CDate("12/31/" & CStr(intYear - 1))
dtmToday = DateAdd("d", dtmStart, intDay)

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 07-06-2009   #4 (permalink)
ekkehard.horner


 
 

Re: How to reverse DatePart("y",Date) ?

Pegasus [MVP] schrieb:
Quote:

> "Brad" <brad@xxxxxx> wrote in message
> news:9c79020e-4d98-44aa-aff2-bc0a8b853acc@xxxxxx
Quote:

>> DatePart("y",Date)
>>
>> This would give an integer day of the year representing today but how
>> do you achieve the opposite?. I am supplied with the int day or year,
>> and need to convert that into a date.... any ideas?
>
> Have a look at the CDate function. The help file script56.chm, which you can
> download from the Microsoft site, has some detailed examples.
>
>
Looking at the CDate function (useful to convert strings to dates) won't
harm, but consider using DateSerial (more convenient if you have at least
one integer for year, month, or day). There is a TimeSerial function too.
My System SpecsSystem Spec
Old 07-06-2009   #5 (permalink)
Brad


 
 

Re: How to reverse DatePart("y",Date) ?

On Jul 6, 6:40*pm, "Richard Mueller [MVP]" <rlmueller-
nos...@xxxxxx> wrote:
Quote:

> "Brad" <b...@xxxxxx> wrote in message
>
> news:9c79020e-4d98-44aa-aff2-bc0a8b853acc@xxxxxx
>
Quote:

> > DatePart("y",Date)
>
Quote:

> > This would give an integer day of the year representing today but how
> > do you achieve the opposite?. I am supplied with the int day or year,
> > and need to convert that into a date.... any ideas?
>
> Perhaps:
>
> intDay = DatePart("y", Date())
> dtmToday = DateAdd("d", #12/31/2008#, intDay)
>
> Or:
>
> intDay = DatePart("y", Date())
> intYear = Year(Date())
> dtmStart = CDate("12/31/" & CStr(intYear - 1))
> dtmToday = DateAdd("d", dtmStart, intDay)
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab -http://www.rlmueller.net
> --
The date, function was an example of the DatePart way of doing it ,
the task is to convert like as follows:

Input: 1
Output: 1/1/2009

or

Input: 30
Output: 30/1/2009

And so on through 365 days of the year, so as to reverse the day of
the year DatePart function.

Thx for replies so far, will look into the CDate method
My System SpecsSystem Spec
Old 07-06-2009   #6 (permalink)
ekkehard.horner


 
 

Re: How to reverse DatePart("y",Date) ?

Brad schrieb:
Quote:

> DatePart("y",Date)
>
> This would give an integer day of the year representing today but how
> do you achieve the opposite?. I am supplied with the int day or year,
> and need to convert that into a date.... any ideas?
If you follow Pegasus' good advice and read about CDate(), you'll
find that CDate()

(1) assumes a valid date expression as parameter; the docs recommend
to check that presupposition by using IsDate()

(2) depends on the current locale settings

(3) is first and foremost meant to convert strings to dates

(4) is a 'second best' way to specify dates:

The following example uses the CDate function to
convert a string to a date. In general, hard coding
dates and times as strings (as shown in this example)
is not recommended. Use date and time literals (such as
#10/19/1962#, #4:45:23 PM#) instead.

so you have been warned

So you may be surprised at the results when you concatenate your (day)
numbers into strings and feed them to CDate.

The DateSerial() function "Returns a Variant of subtype Date for a
specified year, month, and day" - with no dependency on the locale
and no attempts to parse a string 'intelligently'. When you have one
or more *numbers* and need (a) date(s), ist's easier and more reliable
to use this function.

Dim aTests, vItem
aTests = Array( "1.1.1", "1.2.1", "34.1.1", "1.1.34", "13.13.13" )
WScript.Echo "CDate() does its utmost to convert a string to a date"
For Each vItem In aTests
WScript.Echo vItem, "=>", CDate( vItem ), CStr( IsDate( vItem ) )
Next
aTests = Array( 1, 30, 77, 364, 365, 366 )
WScript.Echo "DateSerial is much more predictable"
For Each vItem In aTests
WScript.Echo vItem, "=>", DateSerial( 2009, 1, vItem )
Next

output

=== DontUseCDate: don't use CDate() ====================
CDate() does its utmost to convert a string to a date
1.1.1 => 01.01.2001 Wahr
1.2.1 => 01.02.2001 Wahr
34.1.1 => 01.01.1934 Wahr
1.1.34 => 01.01.1934 Wahr
13.13.13 => 09.07.2259 Falsch
DateSerial is much more predictable
1 => 01.01.2009
30 => 30.01.2009
77 => 18.03.2009
364 => 30.12.2009
365 => 31.12.2009
366 => 01.01.2010
=== DontUseCDate: 0 done (00:00:00) ====================


My System SpecsSystem Spec
Old 07-06-2009   #7 (permalink)
Brad


 
 

Re: How to reverse DatePart("y",Date) ?

I quickly put this test together , seems to do what I need.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<html>
<body>

<script type="text/vbscript">

sub Submit_OnClick()

Dim TheForm
Set TheForm = document.form1
num = TheForm.txt.value
dtDate = "01/01/" & Year(Date)

While x = false
comp = DatePart("y",CDate(dtDate))

If CInt(num) = CInt(comp) Then
x = true
toDate = CStr(dtDate)
Else
dtDate = DateAdd("d", 1, dtDate)
End If
WEnd

Msgbox toDate
End sub
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<input name="txt" type="text" id="txt" value="9" />
<input type="button" name="Submit" id="Submit" value="Button" />
</form>
</body>
</html>
My System SpecsSystem Spec
Old 07-06-2009   #8 (permalink)
ekkehard.horner


 
 

Re: How to reverse DatePart("y",Date) ?

Brad schrieb:
Quote:

> I quickly put this test together , seems to do what I need.
>
You are responsible for your code and free/entitled to write it your
way. Now I believe, that there are better ways to solve your problem
(determining a date given a number of days relative to the first day
of the current year).

The user input (string: TheForm.txt.value) has to be validated and
converted to a number (Int or Long). Depending on the details of
your specification you may refuse negative numbers and/or numbers
greater than 364/365. After that

dtToDate = DateSerial( 2009, 1, nDays )

will do the right thing without any (infinite) loops, type mismatches,
and wasteful increments.

Test your script with TheForm.txt.value =

""
"this is not a number
"-1"
"366"
[...]
Quote:

> sub Submit_OnClick()
>
> Dim TheForm
> Set TheForm = document.form1
> num = TheForm.txt.value
num is a string; because there is no validation, you risk
type mismatches in "CInt(num)"; because you don't convert it
here, you have to do that repeatedly in the loop
Quote:

> dtDate = "01/01/" & Year(Date)
dtDate is string - you are guilty of type prefix fraud; because you
don't convert it here, you have to do that repeatedly in the loop
Quote:

> While x = false
The VBscript Docs say:
The While...Wend statement is provided in VBScript for those
who are familiar with its usage. However, because of the lack
of flexibility in While...Wend, it is recommended that you use
Do...Loop instead.
(and they are right)
"While x = false" is just clumsy for "Do Until x"
Quote:

> comp = DatePart("y",CDate(dtDate))
First time around dtDate is a string, so the conversion is correct;
but after that dtDate is a date (dtDate = DateAdd("d", 1, dtDate),
so the conversion is wasteful; comp is a number.
Quote:

> If CInt(num) = CInt(comp) Then
CInt(num) fill fail *here* if the user entered garbage; both CInt()
calls are wasteful
Quote:

> x = true
> toDate = CStr(dtDate)
misleading type prefix; why convert at all?
Quote:

> Else
> dtDate = DateAdd("d", 1, dtDate)
Programmers are lucky that CPUs can't complain. Even if
you don't like DateSerial(), you could use
DateAdd( "d", <!valid! user input>, dtDate )
Quote:

> End If
> WEnd
>
> Msgbox toDate
> End sub
[...]
My System SpecsSystem Spec
Old 07-07-2009   #9 (permalink)
Brad


 
 

Re: How to reverse DatePart("y",Date) ?

dtToDate = DateSerial( 2009, 1, nDays )

Appears to be exactly what I was looking for, danke!
My System SpecsSystem Spec
Old 07-07-2009   #10 (permalink)
Dr J R Stockton


 
 

Re: How to reverse DatePart("y",Date) ?

In microsoft.public.scripting.vbscript message <9c79020e-4d98-44aa-
aff2-bc0a8b853acc@xxxxxx>, Mon, 6 Jul 2009
08:56:34, Brad <brad@xxxxxx> posted:
Quote:

>DatePart("y",Date)
>
>This would give an integer day of the year representing today but how
>do you achieve the opposite?. I am supplied with the int day or year,
If Jan 1 = 1, as opposed to 0, that is the ISO 8601 Ordinal Date,
Quote:

>and need to convert that into a date.... any ideas?

There seem to be 3 rules to remember when searching for hoe to do date
calculations in VBScript, and I'm not sure which is the most important :

(1) Consider how DateSerial can be used,
(2) See what Ekkehard Horner says,
(3) Don't over-use DatePart and DateAdd.

You can also look in <URL:http://www.merlyn.demon.co.uk/vb-dates.htm>.
Don't use DatePart for Week Number.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to prevent that the lower part of the characters <g, j, p, q, y> are "cut off" at the four top lines <From:, Date:, To:, Subject:>?? Vista mail
Blank "Date Taken / Date Modified" field in Vista Vista file management
Reverse "Don't ask me again" for a device detection? Vista hardware & devices
How to insert the "modified time" attribute in "date taken" attribute in batch mode-in vista or theough a software? Vista file management
How to insert the "modified time" attribute in "date taken" attrib Vista music pictures video


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46