none
How to determine the start and ending dates of the current week RRS feed

  • Question

  • I'm trying to create a report of all items in an unbound datagridview with dates in the current week. I tried the following, after which I was going to see if "Today" was after WeekStart and before WeekEnd. I tried this, to get the start and ending dates for the current week:

            Dim WeekStart As Date = Date.Today - Weekday(Date.Today) + 1
            Dim WeekEnd As Date = Date.Today - Weekday(Date.Today) + 7

    which raises an error saying you can't use "subtract" with dates. Well, I thought you could to that, but maybe not. Anyway, I'm stuck for now. Any solution to this first issue, and then maybe someone knows of an even simpler solution. Thanks.


    Robert Homes

    Tuesday, October 9, 2018 2:01 PM

Answers

  • Try the following

    Dim dayOfWeek = CInt(DateTime.Today.DayOfWeek)
    Dim startOfWeek = DateTime.Today.AddDays(-1 * dayOfWeek)
    Dim endOfWeek = DateTime.Today.AddDays(7 - dayOfWeek).AddSeconds(-1)
    Console.WriteLine(startOfWeek)
    Console.WriteLine(endOfWeek)

    10/7/2018 12:00:00 AM
    10/13/2018 11:59:59 PM


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Robert Homes Tuesday, October 9, 2018 2:46 PM
    Tuesday, October 9, 2018 2:11 PM
    Moderator
  • Hi Robert,

    I would suggest the following but have not fully tested it e.g. you should try rigging it up for say Febuary and also test for leap years.

    Dim FirstDay = DateSerial(Today.Year, Today.Month, 1)
    Dim LastDay = DateSerial(Today.Year, Today.Month + 1, 0)
    In regards to lost post, there are no hidden post, if there were as a moderator I would see it. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Robert Homes Tuesday, October 9, 2018 6:24 PM
    Tuesday, October 9, 2018 3:19 PM
    Moderator

All replies

  • Try the following

    Dim dayOfWeek = CInt(DateTime.Today.DayOfWeek)
    Dim startOfWeek = DateTime.Today.AddDays(-1 * dayOfWeek)
    Dim endOfWeek = DateTime.Today.AddDays(7 - dayOfWeek).AddSeconds(-1)
    Console.WriteLine(startOfWeek)
    Console.WriteLine(endOfWeek)

    10/7/2018 12:00:00 AM
    10/13/2018 11:59:59 PM


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Robert Homes Tuesday, October 9, 2018 2:46 PM
    Tuesday, October 9, 2018 2:11 PM
    Moderator
  • Karen, thanks for your code, it worked. You could give me a little more help by sending a revision of your code to provide the first and last days of the current Month, and for the current Year.

    BTW, I had started to send you a reply earlier, but for some reason the text of that Reply got lost or hidden. I tried to send another reply that same session but was told I had to close the other reply first -- but I couldn't find it! I ended up quitting the site and coming back in and retyping my original reply. Maybe you could tell me how to retrieve an unfinished reply that got "lost" like that.


    Robert Homes

    Tuesday, October 9, 2018 2:52 PM
  • Hi Robert,

    I would suggest the following but have not fully tested it e.g. you should try rigging it up for say Febuary and also test for leap years.

    Dim FirstDay = DateSerial(Today.Year, Today.Month, 1)
    Dim LastDay = DateSerial(Today.Year, Today.Month + 1, 0)
    In regards to lost post, there are no hidden post, if there were as a moderator I would see it. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Robert Homes Tuesday, October 9, 2018 6:24 PM
    Tuesday, October 9, 2018 3:19 PM
    Moderator
  • Karen,

    Haven't checked it out thoroughly or tested it, but it does seem to work okay with the date I've got. I'll let you know later how it did when I've tested it better. Thanks!


    Robert Homes

    Tuesday, October 9, 2018 6:25 PM
  • Karen, I've got an unrelated question. Bear with me first, while I provide some background.

    I am strictly an amateur in programming, although I've done a lot of it. Most of it is on a basic level, though, and I've never even gotten fully acquainted with Classes. I used to know how to program databases, using the old "DAO" system. But ADO was too much for me. The program I'm working on now is designed to keep track of an attorney's hours working on legal cases. It uses two datagridviews, one for the Case list, another for the list of Time entries for each case. Both grids are UNBOUND - they don't connect to a database. They keep, load, and save data in text files in the User's Documents folder. When the User wants to create a bill for his time, the program uses Excel for that.

    That was all fine when I started out. Now that I've expanded the thing further, I think I'm going to need to put all the data I'm collecting into a database.

    Okay, now the issue is how can I get the database done. I know enough to be annoying, but not enough to do a good job of it, or really any job at all.

    My question is, can I get you or one of the other "gurus" on this site to help me create the database? I'm willing to pay them for doing it, but not a huge amount. I'm retired and living on a "nest egg" saved up while working, and I can't afford to deplete that too much. (Also, my wife's interests need to be considered.) I'm doing the program for my son, an attorney working in New Orleans. But eventually I'd like to be able to sell the thing, and could use some help on how to sell/market it.

    I was very surprised to learn recently that the gurus who frequent your site don't get paid for helping others like me. That was a shock, considering how much time they must devote to it. I've not explored the issue of whether any of them might be willing to help me with my program, but finally decided I had nothing to lose by asking.

    What information or advice can you give me? Thanks for responding.


    Robert Homes

    Tuesday, October 9, 2018 6:39 PM
  • Hi Robert,

    What follows is a high level guide to moving to a database driven solution. I didn't get deep into details, just a dip in the perverbal code pool. Note that I have code samples as mentioned below which I can provided later today (currently at work). These code samples are all SQL-Server based.


    EDIT Here is one of the code samples.

    Addressing moving from text file base data source to a real database such as SQL-Server Express.

    The basics steps are

    Use Visual Studio 2017 Community Edition (it’s free) or a higher edition.

    Installing SQL-Server Express (It’s free)

    Installing SQL-Server Management Studio (it’s free) which is a IMHO the only tool and must have to creating database, tables and writing your SQL (known as TSQL for SQL-Server).

    Create your database and tables you want to store imported data from text files.

    Learn how to write CRUD operations in VB.NET for SQL-Server (I can point to some of my code samples)

    Write code to import text file data (a precursor is to have basic knowledge of working with files and databases)

    From here many would say learn how to use Entity Framework but I’m not doing this, instead the logic of data operations will be simple as done/mentioned above).

    Once the above is done you need to simply connect everything together for the user interface. I have code samples for this too.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, October 9, 2018 7:17 PM
    Moderator
  • Karen, I have a lot to say and don't know how to go through it all! First, I want to thank you for taking the time to give me the info you have provided. I went ahead and downloaded the two applications you mentioned - SQL Server Express and SQL Server Management Studio. (Not that I know much about them!)

    As far as "Learn how to write CRUD operations in VB.NET for SQL-Server" goes, I know nothing, not even what "CRUD" stands for.

    Like I said before, I did write database stuff in VB previously using Access databases, using the DAO procedures that used be be available in VB. So I do know a little about databases, and can guess at what SQL must be like. (At one point I even started in on it then backed off due to other pressing matters.) But it sure looks like a big learning curve to me.

    I am 76 years old!!! How much time I have left to get this here little program I've worked on finished is a big unknown. Father Time could strike me down at any time, especially since I've gotten really overweight with the stress of programming day & night.

    As I told you, my son (Justin) is an attorney in New Orleans. I was an attorney myself until I retired a few years ago. While I was practicing law, I wrote a whole bunch of VB programs to use in my law office. In fact, I had "designed" (but never finished) a full-fledged system for law offices, including managing contacts, case information, pleadings, and on and on. I was going to tie all the pieces (programs) together in something I called "CaseForm". I hoped that when I retired I would have the time to finish some of that. The program I'm working on now I call "Timer" to keep track of lawyer's time and billing, is the closest one to getting finished enough to actually be distributed to others. I've also (almost) finished "POL", my program for keeping track of "Points Of Law". I also have a program designed to manage all the interactions of lawyers and their staffs with Microsoft Word. I call that "Favorites" because its primary purpose is to handle any files which are the lawyer's "favorites" at any point in time. But it does a whole lot more. It also includes Access database with tables for Cases and Contacts. I could go on and on. You get the idea, I think.

    The big problem is that I am not a good enough programmer to get this stuff truly finished, bug free, and marketable. I also know NOTHING about how to market any of this stuff either. When I used the programs in my law office, they worked, because I was always around to fix or work around the bugs that were always there.

    Where am I going with this?? Sometimes I get lost in the details. Anyway, I need to get back to one of my questions about your last message. You were trying to get me ready to learn SQL. I checked your resume/profile on this site and see you are an "Instructor". So my question is, if I am to try your SQL stuff, will you be there to instruct me in it as I go along? If so, I'd be a lot more receptive to trying it out. And I'd be willing to pay you a modest fee for your time.

    My next question is a little trickier. I started out asking if I could find some "guru" on the site to help me design the database for my Timer app (or rather, do it themselves!) You never did answer that question! You veered off into me doing it on my own. But I still would like to know -- can I find someone on this site to do the database stuff for me? Maybe you, yourself, would be interested. If not, can I see if someone else there is? My reluctance to find out about that was due to the fear that maybe Microsoft would not want people (me) to use their site as a "headhunter" device. But I finally have asked about that, and would like your comment.

    So -- if I am going to have to do the SQL stuff myself, would you be around to help me learn it? And if so, what would you charge me for doing that? And if I'm not going to do it myself, would you be interested in doing it for me for a price, or allow me to find someone else on your side to do it?

    Thanks so much for your time!!

    Robert Homes, rohomes@gmail.com


    Robert Homes

    Tuesday, October 9, 2018 11:52 PM
  • Hi Robert,

    I would suggest the following but have not fully tested it e.g. you should try rigging it up for say Febuary and also test for leap years.

    Dim FirstDay = DateSerial(Today.Year, Today.Month, 1)
    Dim LastDay = DateSerial(Today.Year, Today.Month + 1, 0)
    In regards to lost post, there are no hidden post, if there were as a moderator I would see it. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Without looking into the DateSerial function, would you not want to get 2019 in the first argument of LastDay if we were in Dec of this year? I am probably wrong, but it looks like you would be a year off otherwise.

    Kind of like this is what I have in mind

            Dim StartDate As Date = New Date(Now.Year, Now.Month, 1)
            Dim EndDate As Date = New Date(StartDate.AddMonths(1).Year, Now.Month + 1, 1).AddDays(-1)


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi



    • Edited by Gtripodi Wednesday, October 10, 2018 3:06 AM
    Wednesday, October 10, 2018 3:04 AM
  • Karen, thanks for your help. I thought I had learned a lot about the date functions awhile back, but I seems to have forgotten them. It's a lot of stuff to remember.

    BTW, that's the best quote I have seen in a long, long time. Imagine living every day knowing/thinking you were going to die that day. Well, I guess you could take that two ways - (1) the good view is that you would invest everything in what you do that day, but (2) knowing you were actually going to die that day, well, that could be a bummer, know what I mean. In other words, the first part - "AS IF" - is a very important part of it. Anyway, love the thing. And that's coming from a real quote lover. See my collection on my web site, www.papacelestin.com. I think I'll add Gandhi's message to my collection tomorrow.

    Whenever I think of quotes, I seem to focus for some reason on John Coltrane's two quotes on my site:

    I want to produce beautiful music, music that does things to people that they need.

    and

    Considering the great heritage in music that we have – the work of the giants of the past, the present, and the promise of those who are to come – I feel that we have every reason to face the future optimistically.

    Why do those two stand out so much for me? Don't know, but they seem to accumulate force and power over time, as simple as they are. Makes you think what a great man "The Train" really was, in spite of his addiction.

    Sometimes I think that you could build a whole system of beneficial psychology just on the great quotations from the great minds of the past.



    Robert Homes


    • Edited by Robert Homes Wednesday, October 10, 2018 6:24 AM
    Wednesday, October 10, 2018 6:22 AM