![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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? download from the Microsoft site, has some detailed examples. |
My System Specs![]() |
| | #3 (permalink) |
| | 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? 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 Specs![]() |
| | #4 (permalink) |
| | 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. > > 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 Specs![]() |
| | #5 (permalink) |
| | 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 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 Specs![]() |
| | #6 (permalink) |
| | 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? 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
| | #8 (permalink) |
| | Re: How to reverse DatePart("y",Date) ? Brad schrieb: Quote: > I quickly put this test together , seems to do what I need.> 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 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) don't convert it here, you have to do that repeatedly in the loop Quote: > While x = false 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)) 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 calls are wasteful Quote: > x = true > toDate = CStr(dtDate) Quote: > Else > dtDate = DateAdd("d", 1, dtDate) you don't like DateSerial(), you could use DateAdd( "d", <!valid! user input>, dtDate ) Quote: > End If > WEnd > > Msgbox toDate > End sub |
My System Specs![]() |
| | #9 (permalink) |
| | Re: How to reverse DatePart("y",Date) ? dtToDate = DateSerial( 2009, 1, nDays ) Appears to be exactly what I was looking for, danke! |
My System Specs![]() |
| | #10 (permalink) |
| | 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, 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 Specs![]() |
![]() |
| 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 | |||