How do I format time in SSRS to HH:MM AM/PM format? - datetime

I was tasked with augmenting the following code to display the time without seconds, and with AM/PM:
=IIF(Fields!New_Date.Value <> "NO CHANGE", FormatDateTime(IIF(Fields!New_Date.Value = "NO CHANGE","1/1/12",Fields!New_Date.Value),DateFormat.ShortDate), "") &
IIF(Fields!New_Time.Value <> "NO CHANGE",FormatDateTime(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value),DateFormat.ShortTime), "")
In realizing that FormatDateTime was insufficient for what I was trying to do, I found the following did not work (just looking at the snippit that relates to the time fields), :
Format(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value),"HH:mm tt")
Or this
Format(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value),"HH:MM tt")
I'm getting the format codes from here.
What am I doing wrong?

So I found that because I was essentially trying to format a textbox, SSRS never really 'knew' that it was a time field. While I'm not 100% sure my reasoning is correct, it does explain why the following works:
Format(CDate(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value)),"hh:mm tt")

If your data is in simple textbox(or table cell) you have easer solution.
right click on the textbox > properties :
use the following:

The other answers are correct, but the key difference to know with the format is:
h:mm tt --> 12-hour clock (e.g. 5:30 PM)
hh:mm tt --> 12-hour clock with a leading zero, if necessary (e.g. 05:30 PM)
H:mm --> 24-hour clock (e.g. 1:30 for 1:30 AM)
HH:mm --> 24-hour clock with a leading zero, if necessary (e.g. 01:30 for 1:30 AM)

From Text Box Properties -> Number -> Custom Category:
Try to input this:
dd-MMM-yy hh:mm tt

Related

MSACCESS Form VBA Open Where Clause - US v AU Date Formatting

Morning all,
This should be a simple fix and I have searched around and found things that appear they should work, but they don't seem to though...
So - it's a simple "Open this form and show only records from This date" function and, you guessed it, good ol' US of A date formatting is the problem and no matter which way I try it nothing is solving the problem...
So - here's the form code...
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "# " & NEWDATE & " #"
to find NEWDATE...
I strip the date components from a couple of combo boxes (CBOYear & CBOMonth) and a field that represents the day of the month (data will be in the format "1st" or "2nd" etc...)
I concatenate the individual components...
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
A msgbox NEWDATE popup results in the correct data (ie 1/3/18)
But when I run the code it either selects everything or nothing... the only time the function works is when the day field is 13 or higher (ie 13/3/18) - thus it can determine the correct format to work with.
I'm sure the correct answer is already here somewhere - the correct question however may be something I have not thought to search for.
I have tried to use DATEVALUE() before and within the form open code. I have tried to hard code the US format to see if that works...
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "#3/1/18#"
No good... shows all records - same if I hard code for the AU format #1/3/18#
My PC is set up for AU format dd/mm/yy
So... Which function do I need to use to convert the AU Date string into US Date string prior to calling the Openform, so the correct records open? OR is there a better way to write the WHERE clause in the Docmd line to achieve this result? - Or do I need to reset all my date fields to US format and convert them back to AU on the forms only?
Thanks for the feedback...
In VBA and VB6, date-literals using the # syntax are always in M/dd/yyyy format, regardless of the user's date format settings. This is a legacy of VBA/VB6's development in the USA before localization was a concern (and the USA is the only country to use the illogical MM/dd/yyyy format).
Note it's M/dd/yyyy and not MM/dd/yyyy - so omit any leading zeroes.
This is documented here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/date-data-type
The same rule also applies to date-literals in Access SQL (not the same thing as VBA/VB6). Fortunately in SQL Server you always use the ISO 8601 yyyy-MM-dd format in quotes... shame about Access though.
You simply need a properly formatted string expression for your date value - and the correct syntax:
Dim NEWDATE As Date
Dim WhereCondition As String
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
WhereCondition = "[NameOfYourDateField] = #" & Format(NEWDATE, "yyyy\/mm\/dd") & "#"
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , WhereCondition
Your first problem is that you're not comparing NewDate to something.
Your second problem is that you're using string concatenation to filter by a date, and that results in the wrong date.
You can view this answer for the different ways to use parameters in Access.
Your final code should probably look something like this:
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
DoCmd.SetParameter("NewDateParam", NEWDATE)
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "SomeDateField = NewDateParam"
Or, if there might be time stored with the date:
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
DoCmd.SetParameter("NewDateParam", NEWDATE)
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "SomeDateField - NewDateParam < 1"

Adding random time to DateTime value

I would like to add a random time between 1-30 minutes to a date, but ColdFusion doesn't seem to like it. It adds the same amount of minutes to the Now() value no matter how many times I run the following code. I can't figure out why.
<cfset DateFuture = DateTimeFormat(DateAdd('n', RandRange(1, 30), Now()), 'yyyy-mm-dd HH:mm:ss.l')/>
<cfoutput>#DateFuture#</cfoutput>
I need the yyyy-mm-dd HH:mm:ss.l DateTimeFormat because this what my datetime values in SQL Server look like which is where I plan to insert my future date.
If I run the above code I keep getting this output:
2017-11-25 22:11:24
and then suddenly it will change to 2017-11-25 21:11:16 which is taking away a whole hour when I only want to add to the time!
It makes no sense why its behaving like this. I am in the UK but am using international date format in the default format of SQL Server which is like above.
UPDATE: Its a typo mistake! The DateTimeFormat should be yyyy-mm-dd HH:nn:ss.l. 'nn' is minutes, not 'mm'. D'oh!
If you use the minutes, 1 to 30 range won't give you many values.
You'll make it somewhat more random using seconds [600 to 1800] or miliseconds [600000 to 1800000].
<cfset miliSeconds = RandRange(600000, 1800000) />
<cfdump var="#miliSeconds#" />
<cfset DateFuture = DateTimeFormat( DateAdd('l', miliSeconds, now()), 'yyyy-mm-dd HH:nn:ss.l')/>
<cfdump var="#DateFuture#" />
Run Code:
https://trycf.com/gist/f26ff8edbe1736e453ded06d5adf5076/lucee5?theme=monokai
The whole problem was down to a typo. It should be HH:nn:ss. Notice the nn and not mm for minutes.

report builder 3.0 date timestamp expression

I have a report that uses the folowing expression in a date timestamp field to format the date as either US format or European format depending on a Language prompt the user selects:
=iif(Parameters!LANG.Value = "EN", ToDateTime(Fields!TRANS_DATE.Value).ToString("MM/dd/yyyy"), ToDateTime(Fields!TRANS_DATE.Value).ToString("dd-MM-yyyy"))
If he user selects English, April 1st appears as 04/01/16. If they select any other format, the date appears as 01-04-16. Works great.
I would like to do the same thing on another field but I would like to leave the time in the results. So, if the user selects English, the results would display 04-01-16 11:15:23 AM. Otherwise, I would like to see 01-04-16 11:15:23 AM.
Can I modify the expression above to do the same thing but leave the hours and minutes and seconds in the result?
Thanks for your help.......
You need to append this to your datetime format string:
"hh:mm:ss tt"
So in your case:
=iif(Parameters!LANG.Value = "EN", ToDateTime(Fields!TRANS_DATE.Value).ToString("MM/dd/yyyy hh:mm:ss tt"), ToDateTime(Fields!TRANS_DATE.Value).ToString("dd-MM-yyyy hh:mm:ss tt"))

ASP.NET "String was not recognized as a valid DateTime."

First off, I realize there's a million pages discussing this already. I have looked at least a hundred of them but cannot seem to make this work. My date and time is presented as a string, compiled from javascript to grab client's local time. It is formatted like this: 7/11/2015 8:34 PM.
I currently have:
Dim datetimeformated = DateTime.ParseExact(lblDateTime.Text, "MM/dd/yyyy HH:mm tt", CultureInfo.InvariantCulture)
I have tried many different variants, but this should be correct I think, yet it does not work. Any help is greatly appreciated. TIA
The correct format for your case is: M/dd/yyyy h:mm tt, and perhaps even, M/d/yyyy h:mm tt, if you can have the day of the month as a single digit.
Explanation: Why your format string didn't work.
MM: means that you must always have 2 digits for the month, clearly not the case in your example.
dd: again, means that you must always have 2 digits for the day of the month. Is that the case? Adjust the parameter if needed.
HH: This actually means that you are expecting the hour value as 2-digits using the 24-hour clock (00-23), which is clearly wrong on both accounts. You can have a single digit, and you are not using the 24-hour clock, because you are using the AM/PM designator.
Relevant documentation link: Custom Date and Time Format Strings.

Format Datetime in Business Objects

So I know I can use =FormatDate(MyDate ,"mm/dd/yy") to turn a date into a string. I am then trying to add on a time:
=FormatDate(AdminDate ,"mm/dd/yy") + MyTime
Which works, however, I need to format this back to a datetime field (as I need to compare against a preexisting datetime field). I try using Todate(), but the documentation is very light, and provides very little on what is acceptable in the formatting of the date area, and nothing in the way of time!
I have attempted:
=ToDate(FormatDate(MyDate ,"mm/dd/yy HH:mm:ss") + MyTime ,"mm/dd/yy HH:mm:ss")
but this will only work when there is no time (and it just nulls out the time) and any row with a time will return a #ERROR
Anyone have an insight on formatting datetimes?
Thanks
The correct way to use the FormatDate command to output date and time components together in 12 hour format is:
=FormatDate(AdminDate; "MM/dd/yy hh:mm:ss a")
and in 24 hour format is:
=FormatDate(AdminDate; "MM/dd/yy HH:mm:ss")
Note MM is used in months and mm in minutes.

Resources