Format datetime day with st, nd, rd, th - datetime

I'm creating a report in SSRS and across the top I have a header with a placeholder for "Last Refreshed" which will show when the report last ran.
My function in the placeholder is simply this:
=Format(Now, "dddd dd MMMM yyyy hh:mm tt")
Which looks like this:
Monday 22 September 2015 09:46 AM
I want to format the day value with the English suffix of st, nd, rd and th appropriately.
I can't find a built in function for this and the guides I've looked at so far seem to describe doing it on the SQL side with stored procedures which I don't want. I'm looking for a report side solution.
I thought I could get away with an ugly nested IIF that did it but it errors out despite not giving me any syntax errors (whitespace is just for readability).
=Format(Now, "dddd " +
IIF(DAY(Now) = "1", "1st",
IIF(DAY(Now) = "21","21st",
IIF(DAY(Now) = "31","31st",
IIF(DAY(Now) = "2","2nd",
IIF(DAY(Now) = "22","22nd",
IIF(DAY(Now) = "3","3rd",
IIF(DAY(Now) = "23","23rd",
DAY(Now) + "th")))))))
+ " MMMM yyyy hh:mm tt")
In any other language I would have nailed this ages ago, but SSRS is new to me and so I'm not sure about how to do even simple string manipulation. Frustrating!
Thanks for any help or pointers you can give me.
Edit: I've read about inserting VB code into the report which would solve my problem, but I must be going nuts because I can't see where to add it. The guides say to go into the Properties > Code section but I can't see that.

Go to layout view. Select Report Properties.Click on the "Code" tab and Enter this code
Public Function ConvertDate(ByVal mydate As DateTime) as string
Dim myday as integer
Dim strsuff As String
Dim mynewdate As String
'Default to th
strsuff = "th"
myday = DatePart("d", mydate)
If myday = 1 Or myday = 21 Or myday = 31 Then strsuff = "st"
If myday = 2 Or myday = 22 Then strsuff = "nd"
If myday = 3 Or myday = 23 Then strsuff = "rd"
mynewdate = CStr(DatePart("d", mydate)) + strsuff + " " + CStr(MonthName(DatePart("m", mydate))) + " " + CStr(DatePart("yyyy", mydate))
return mynewdate
End function
Add the following expression in the required field. I've used a parameter, but you might be referencing a data field?
=code.ConvertDate(Parameters!Date.Value)

Right Click on the Textbox, Go To Textbox Properties then, Click on Number tab, click on custom format option then click on fx button in black.
Write just one line of code will do your work in simpler way:
A form will open, copy the below text and paste there to need to change following text with your database date field.
Fields!FieldName.Value, "Dataset"
Replace FieldName with your Date Field
Replace Dataset with your Dateset Name
="d" + switch(int(Day((Fields!FieldName.Value, "Dataset"))) mod
10=1,"'st'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 =
2,"'nd'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 =
3,"'rd'",true,"'th'") + " MMMM, yyyy"

I found an easy way to do it. Please see example below;
= DAY(Globals!ExecutionTime) &
SWITCH(
DAY(Globals!ExecutionTime)= 1 OR DAY(Globals!ExecutionTime) = 21 OR DAY(Globals!ExecutionTime)=31, "st",
DAY(Globals!ExecutionTime)= 2 OR DAY(Globals!ExecutionTime) = 22 , "nd",
DAY(Globals!ExecutionTime)= 3 OR DAY(Globals!ExecutionTime) = 23 , "rd",
true, "th"
)

Related

How to get r to read 'date taken' of a JPG file

I could use some help performing a database correction, in regards to date and time of pictures were taken.
Essentially, the research we perform entails taking many pictures and entering the picture information into a database (we automated this with Microsoft Access). However, I performed a random check of our database and found that several dates and times of the photos were incorrect, and I am attempting to correct this via R as I was unable to correct it via Access.
What I need to do is to is write a script that reads these data, and compiles a list of the date taken information for all photos (there are several thousand). So far the best thing that I've found is
file.info(list.files(
"E:/Whatcom Creek Project/Data/Seal photos/Discovery/Catalog/Phoca vitulina",
recursive = T))
However this returns a list of NA NA for all information. Also, if I manually select one image to run file.info on, it doesn't return date taken (see the picture for the data I am attempting to retrieve)
If anyone has any suggestions I am all ears. Thanks in advance!!
-Ian
enter image description here
maybe still of any use?
i saw this message. I solved this a long time ago (2011) with a very basic VB6 code. The job is still working, but it is not applicable to all '.jpg files'. It depends with what camera the picture has been taken, but mostly pictures taken from a smartphone and classic camera's are returning the date the picture has been taken (not usable for whatsapp images or edited images).
The code is here below, and it is very basic code (but i hope it still can help or give idea's, and ... any new idea's are welcome too) :
'Reading of "Date Picture Taken" from .jpg file
'-----------------------------------------------
Debug.Print ">>>>>==== Start Read of .jpg-file ===== "; Date; " ===== "; Time; " ====<<<<<"
X = 0
DatePictureTaken = ""
TimePictureTaken = ""
FOUNDdatum = False
SWdatum = False
TELdatum = 0
Foto = FOLDER & "\" & tabFileName(FotoNr)
Open Foto For Binary Access Read As #1
'get startposition of the field "date picture taken"
Do
X = X + 1
Get #1, X, MyByte ': Debug.Print Chr(MyByte);
DoEvents
If Chr(MyByte) = ":" Then
'Debug.Print
SWdatum = True
Get #1, X + 3, MyByte
'Debug.Print "x+03="; Chr(MyByte)
If Chr(MyByte) <> ":" Then SWdatum = False 'Debug.Print "x+03="; Chr(MyByte)
Get #1, X + 9, MyByte
'Debug.Print "x+09="; Chr(MyByte)
If Chr(MyByte) <> ":" Then SWdatum = False 'Debug.Print "x+09="; Chr(MyByte)
Get #1, X + 12, MyByte
'Debug.Print "x+12="; Chr(MyByte)
If Chr(MyByte) <> ":" Then SWdatum = False 'Debug.Print "x+12="; Chr(MyByte)
'if a ':' is on the 3 locations (found above) then it is a date!
If SWdatum _
Then
TELdatum = TELdatum + 1
End If
'the 3e date is the date the picture has been taken
If TELdatum = 3 _
Then
FOUNDdatum = True
X = X - 4
Exit Do
End If
X = X + 12
End If
Loop Until EOF(1) 'Or X = 32765
If FOUNDdatum = False Then Close 1: End
BPdatum = X
EPdatum = X + 9
BPuur = X + 11
EPuur = X + 15
For X = BPdatum To EPdatum
Get #1, X, MyByte
'Debug.Print Chr(MyByte)
If Chr(MyByte) <> ":" _
Then
' DatePictureTaken = DatePictureTaken & "/"
'Else
DatePictureTaken = DatePictureTaken & Chr(MyByte)
End If
Next X
For X = BPuur To EPuur
Get #1, X, MyByte
'Debug.Print Chr(MyByte)
If Chr(MyByte) <> ":" _
Then
' DatePictureTaken = DatePictureTaken & "/"
'Else
TimePictureTaken = TimePictureTaken & Chr(MyByte)
End If
Next X
'Debug.Print "Date Picture Taken = "; DATUM
tbxDatum.Text = DatePictureTaken
tbxUur.Text = TimePictureTaken
Close 1
End Sub

Convert datetime string to a different datetime string in PowerShell v2

I have the following string which is pulled from an entry in a log file.
$d = "19/09/2014 22:41:27"
However, I need to convert it so that it is
2014-09-19 22:41:27
so that I can export it with other sections of the logfile into a MySQL database.
But I can't for the life of me find a way to do this. I was expecting to find something like set-dateFormat, which would simply re-map the components of the string, but it doesn't seem to exist.
I have tried various variations of the following:
$a = "19/09/2014 22:41:27"
$d = [datetime]::ParseExact($a, "dd/MM/yyyy hh:mm:ss", $null)
$e = "{0:yyyymmddhhmmss}" -f [datetime]$d
But everything returns the error:
String was not recognized as a valid DateTime.
What is the best way to get the format I need please?
A quick play around, with the help of a few other blogs provided the following, very similar to your own. There's probably a way to make it a little more streamlined, but it will create the datetime object you need. Manipulating the output after that should be straightforward.
$theDTString = "19/09/2014 22:41:27".toString()
$theDateTimeObject = ([datetime]::ParseExact($theDTString,"dd/MM/yyyy HH:mm:ss",$null))
$theDateTimeObject.year.toString() + "-" + $theDateTimeObject.month.toString() +"-"+
$theDateTimeObject.day.toString() + " " + $theDateTimeObject.Hour.toString() + ":" +$theDateTimeObject.Minute.toString() + ":" + $theDateTimeObject.Second.toString()
The identical answer to your original input is this:
$a = "19/09/2014 22:41:27"
$d = [datetime]::ParseExact($a, "dd/MM/yyyy HH:mm:ss", $null)
$e = "{0:yyyymmddhhmmss}" -f [datetime]$d
It was having an error trying to convert hour 22 as a 12 Hour format.

IsDate validates wrong format

I have these lines
SetLocale(3081)
Response.Write "<p>TEST: " & Date() & " | " & isDate("3/22/2014") & " --> " & GetLocale() & "</p>"
which outputs
TEST: 3/07/2014 | True --> 3081
now correct me if i'm wrong but isn't there only 12 months in a year?, according to IsDate the date i've passed, which should be wrong because i have put in 22 as the month, is valid despite the local settings saying otherwise.
i want to validate the date to be the correct format to insert into the database and if it isn't give a more friendly error, "3/22/2014" will output "Error converting data type varchar to date." when i try to inster it into the database because it's getting by the IsDate check
What have i done wrong here?
Yes, this is indeed a valid date.
Why? Because VBScript is smart/generous/stupid (choose your favorite) enough to treat numbers as dates. And "3/22/2014" can be parsed as formula: 3 / 22 / 2014 = 6.770786313983931e-5
Now take this number and convert to date:
Dim myNumber, myDate
myNumber = 6.770786313983931e-5
myDate = CDate(myNumber)
The variable myDate will be perfectly valid date, which is December 30th 1899, 00:00:06
So bottom line: the value is a date, just not what you expect. You've done nothing wrong, but to really check if a string is valid date you will have to check it yourself, no out of the box methods.

writing csv adds extra lines

I've to write a csv for a third-party upload. They're saying they can't read the csv file created because it has two extra lines of code and doesn't end (when opened in Notepad) on the last character of the last line. That's true - but can anyone tell me why?
Dim _csvLine As New System.IO.StreamWriter(Server.MapPath("~/folder/_" & rptType.SelectedItem.Value.ToString & ".csv"), False)
Dim tb As New StringBuilder
Dim x As Integer = ds.Tables("csv").Columns.Count, y As Integer = 1
For Each row As Data.DataRow In ds.Tables("csv").Rows
For Each col As Data.DataColumn In ds.Tables("csv").Columns
If y <> x Then
tb.Append(Trim(System.Text.RegularExpressions.Regex.Replace(row.Item(col).ToString, "\s+", " ", RegexOptions.IgnoreCase Or RegexOptions.Multiline)) & ",")
y = y + 1
Else
tb.Append(Trim(System.Text.RegularExpressions.Regex.Replace(row.Item(col).ToString, "\s+", " ", RegexOptions.IgnoreCase Or RegexOptions.Multiline)))
tb.AppendLine()
y = 1
End If
Next
Next
_csvLine.WriteLine(Left(tb.ToString, Len(tb.ToString) - 2))
_csvLine.Flush()
_csvLine.Close()
_csvLine.Dispose()
One line is appended by _csvLine.WriteLine, use Write() instead.
tb.AppendLine() adds the second line, try avoiding it on the last line.
You're doing _csvLine.WriteLine( which is gonna add a linebreak to the end of the file.

radio button list selectedItem.Value not working when compared to a string

I am using VB.NET and I can't compare the radio button lists selectedItem.Value to a string, it doesn't work...here is the code: (I have also tried selectedValue it does not work either)
Response.Write("RB1: " + rblOne.SelectedItem.Value + " FML FML FML<br/>")
If rblOne.SelectedItem.Value = "No" Then
Response.Write("Hey there!<BR/>")
pnlR1.Visible = True
If NumberOfAnswers = 7 Then
Score = Score - 10
ElseIf NumberOfAnswers = 6 Then
Score = Score - 15
Else
Score = Score - 20
End If
Response.Write("Score: " + Score.ToString)
End If
Response.End()
If rblOne.SelectedItem.Value = "No" Then is not working, notice the debug statements in there, here is the output:
Why won't it evaluate that rblOne.SelectedItem.Value = "No" !?!?!? I tried rblOne.SelectedValue, that doesn't work, AND I added .ToString to both, which did not help, I even tried it w/ "No".ToString...this doesn't make any sense.
If rblOne.SelectedIndex > -1 AndAlso rblOne.Items(rblOne.SelectedIndex).Value.ToString = "No" Then
'Code to run if the selected list item in the radio button list has a value of "No"
End If

Resources