Ok, so I need to find the date of Monday this week programmatically.
For example, for this week Monday was on the 9th, so the date I need is: 09/11/2009
And when we roll over to next week it needs to calculate: 16/11/2009
I have tried doing this myself but I can't see how to do the arithmetic, thank you.
C#:
date.AddDays(1 - (date.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)date.DayOfWeek));
VB.NET:
date.AddDays(1 - IIf((date.DayOfWeek = DayOfWeek.Sunday), 7, date.DayOfWeek))
Dim thisMonday As Date = Now.AddDays((Now.DayOfWeek - 1) * -1).Date
If today is a Sunday it gives the following Monday otherwise, gives the Monday this week.
Return givenDate.AddDays(1 - CType(IIf((givenDate.DayOfWeek = DayOfWeek.Sunday), 7, givenDate.DayOfWeek), Double))
If givenDate is a Sunday, counts back to the preceding Monday. Includes a CType to cast the IIf result to a Double to work with Option Strict On.
Related
How do I get the first day of the first month of previous year in yyyy-mm-dd format? ie. 2019-01-01.
This is the code I have tried:
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))
You can use DATEFROMPARTS() function in SQL Server for creating date from given year, month and date in integer as shown below. To get the previous year you can use Year() function and subtract 1 from that. First date and month is always 1 so it has been hard-coded here.
declare #IntYear int = Year(Getdate()) - 1 --Previous Year
Select datefromparts(#Intyear, 1, 1)
The output in SSMS is as shown below.
To get the output in the different format you can follow this link.
You seem to be looking to generate a string, not a date. Consider using date functions and string concatenation: you just need to substract 1 year from the current date, and then append '-01-01'
concat_ws('-', year(getdate()) - 1, '01', '01')
Demo on DB Fiddle
You basically have it. You just need the FORMAT function.
SELECT FORMAT (DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)), 'yyyy-MM-dd') as date
I am attempting to grab the current day in the format 01 - 07 or 1 - 7.
This is the query I am using SELECT id FROM tbl_date WHERE weekday = WEEKDAY(NOW) but I seem to get false in return so am not sure I am using the function correctly. The table column weekday contains a number 1 - 7 where 1 is sunday/monday.
Any ideas?
Stupid misstake by me. The correct way would be weekday = WEEKDAY(NOW()) since NOW() itself is a function.
MSSQL can use DATEPART(dw, GETDATE()), MySQL can use DAYOFWEEK()
I am having difficulty with the Day of week number function in SQL. The excel equivalent i use frequently is...
=WEEKDAY("datevalue",1)
This of course will return Sunday = 1, Monday = 2 etc... What is the Access Sql equivalent?
This will suffice to get the answer needed.
weekday(date)
I believe you are looking for DatePart:
DatePart ("w", date)
Reference
This would help
DATEPART(dd, date)
Returns int
It is datepart()
For Example
DatePart ("yyyy", #15/10/1998#) would return 1998
DatePart ("m", #15/10/2003#) would return 10
DatePart ("d", #15/10/2003#) would return 15
I need to subtract a date(stored in db) from current date and return only the year part from it.
Currently for test purposes i am doing some thing like this but the return result is wrong
print DATEDIFF (year ,1993-04-24,getdate())
The return value is 107.
The system date is perfectly right.
Is there something wrong.
You forgot the ' around your date
select DATEDIFF (year , '1993-04-24', GETDATE())
I was wondering if there was a way to directly convert the integer DayOfWeek returns into a string representing the day like Monday, Tuesday etc.
Sample code:
MessageBox.Show(Date.Today.DayOfWeek)
This will return 6 (as of today). Is there a way to directly convert this into Saturday, for example? I don't really care what it really converts it into, but I want to do away with my Select Case:
Select Case Date.Today.DayOfWeek
Case 0
day = "Sunday"
Case 1
day = "Monday"
Case 2
day = "Tuesday"
Case 3
day = "Wednesday"
Case 4
day = "Thursday"
Case 5
day = "Friday"
Case 6
day = "Saturday"
Case Else
day = "Apocalypse: we're all boned."
End Select
Thanks :)
DateTimeFormatInfo.CurrentInfo.GetDayName.
A simpler way:
Dim day As String = Date.Today.DayOfWeek.ToString()
There's a DateTime format for that: dddd
Dim date1 As Date = #08/29/2008 7:27:15PM#
date1.ToString("dddd", CultureInfo.CreateSpecificCulture("en-US"))
With the CultureInfo you can get it in a specific language (it's optional)
For more info:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#ddddSpecifier
DateTime.DayOfWeek doesn't return an integer - it returns an enum of type DayOfWeek. I'd expect that to be converted into the name automatically, but maybe this is a VB subtlety; maybe something to do with using Date instead of DateTime? Try this:
MessageBox.Show(DateTime.Today.DayOfWeek.ToString())
This won't be culture-sensitive though - it will always just display the name of enum value, in English. If that's not good for you, use Zyphrax or itowlson's solution.
Date.Today.DayOfWeek.ToString will give you what you're looking for. Nice and easy.
Just in case others looked at this example. I believe it should be Case 0 for Sunday.
Case 0
day = "Sunday"