VB.Net Custom Mask for Date/Time AM/PM - sql

Here's what I'm trying to do.... I have a field in my application where I am to capture datetime something like this 11/02/2016 12:19 PM (example). I have a field in SQL Server with datatype = smallDatetime. It gets saved like this 2016-11-02 12:19:00. In my app I set the CUSTOM mask to 00/00/0000 90:00 am. Now what I'm trying to do is populate this saved date into my masked textbox but it does not look proper. Is there a way for me to format is somehow so when I try to populate the field in my application it looks properly? This is what I've been trying to figure out...
If Not IsDBNull(Dt("SavedOn")) Then
txtSavedOn.Text = Format(Dt("SavedOn"), "mm/dd/yyyy hh:mm tt"))
End If
Wha thappens is the PM/AM part gets displayed incorrectly and it only shows the M and a number instead of p/a

Change your mask to
00/00/0000 90:00 aa
Also your date format
txtSavedOn.Text = Format(Dt("SavedOn"), "MM/dd/yyyy hh:mm tt"))
(note the capital MM for month, opposed to lower mm for minute)

Just use this code
txtSavedOn.Text = Dt("SavedOn").tostring("MM/dd/yyyy hh:mm tt")

Related

Remove timestamp tag from a date

I have a query comparing 2 tables, one has date in the form of "mm/dd/yyyy" (ex: 1/2/2019) but the other is "mm/dd/yyyy hh:ss:00 am/pm" (ex: 7/2/2019 8:28:00 pm")
In order for the compare to work I need to change the date with the timestamp to the form of "mm/dd/yyyy"
You can just cut the time part:
Fix([DateTimeField]) = [DateField]
A datetime value carries no format. The format is for display only.
Expr1: Format([FieldName],"mm/d/yyyy")
Ended up using this and it worked

Datetime issue in odoo10?

in my custom model I defined the fields
time_from = fields.Datetime(string="Time From", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time_to = fields.Datetime(string="Time To", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
tot_time = fields.Char("Time Difference", compute='_get_time')
this is my compute function
#api.depends('time_from', 'time_to')
def _get_time(self):
t1 = datetime.datetime.strptime(self.time_from, '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(self.time_to, '%Y-%m-%d %H:%M:%S')
if t2<t1:
raise ValidationError('Time To must greater than Time From')
time_diff = (t2-t1)
self.tot_time = time_diff
This is success fully prints time difference.
Time from and time to are mm/dd/yyyy hh:mm:ss format.
How to change this to mm/dd/yyyy hh:mm:ss format
I changed the format like this '%Y-%d-%m %H:%M:%S' .
but it is not getting correct result.
How to change the format?
is it possible to calculate time difference in this format?
The Date and Datetime are saved as strings in the database in a certain format (see the class definition on fields.py. You cannot change the format that is used for the fields in the ORM. If you want to change the format of the date or datetime when you show these fields you can do it not from the code but from:
1) Settings -> Translations -> Find your language and inside you can change the way the fields Date and Datetime are rendered on the client side.
2) If you have a template/report you can use for example<p t-esc="formatLang(time.strftime('%Y-%m-%d %H:%M:%S')" /> or another expression you want to change how the date or datetime will be formed.
3) In the field definition in your xml files you can use custom javascript/widget that will do the rendering.

Access Date Time convert to string. Time is stored as Military, Don't want to convert

I am writing a SQL query for MS Access.
The two fields are DateTime. The start/end time are 22:30/6:30
When I run the following query
SELECT cDate(Format(table1.BeginTime,"hh:mm")),
I get 10:30 PM instead of 22:30. I do not want to convert to 12hrs format. How do I keep the Miliary time format?
So I tried a few things in Access 2013 and it seems that #Bjones has a point.
In SQL Server Format is a clr .net function and format(date, 'hh:mm') will be 12 hour and Format(date, 'HH:mm') will be 24 hour.
But as #Bjones pointed out there may be some question of how your original column is held as text or date or time..... And what I have concluded after trying is that your order of functions is wrong.
cDate(Format(table1.BeginTime,"HH:mm"))
cDate transforms the content to a date but it doesn't format it.
So
FORMAT(cDate(table1.BeginTime), "hh:mm")
or
FORMAT(cDate(table1.BeginTime), "HH:mm")
or
FORMAT(cDate(table1.BeginTime), "Short Time")
should give you what you want if the time is stored as text. If stored as date time you can just drop the cDate all together and stay with
FORMAT(table1.BeginTime, "Short Time")
as #Bjones answered.
You don't tell what the result of the query is intended for.
If you need the date value and to view this in 24 hour format, use:
Select table1.BeginTime
and apply a Format property for the column of h:nn or hh:nn
If you need the date formatted as a string in 24 hour format, use:
Select Format(table1.BeginTime, "hh:nn")
and no Format is needed.
In any case, even though the format string h:mm will work, do use h:nn or hh:nn, as m is for the month.
I think what you need to get rid of is the cDate function as your fields are already in DateTime format.
SELECT Format(table1.BeginTime,"hh:nn")

Converting time to 24h format

First, I convert a time in the format hhmm to hh:nn (24hr format)
Format(TimeSerial(Left([Table].[TIME],2),Right([TABLE].[TIME],2),0),"hh:nn:ss")
Next, I am trying to combine it with a Date, but the time format changes back to 12h automatically:
TABLE.DATE= Format([TABLE].[DATE] & " " & Format(TimeSerial(Left([Table].[TIME],2),Right([TABLE].[TIME],2),0),"hh:nn:ss"),"mm/dd/yyyy hh:nn");
How can I keep the date in a 24h format within this query?
Always separate value handling from display.
If your table fields are of data type Date, you can simply do:
=Table!Date+Table!Time
The format is for display only.

VB.net checking date (year) to see if its an weird year

I'm looping through LDAP data placing some data from that into an MS SQL database. Out of the 7k+ LDAP records, a few have been causing an issue when trying to place the lastlogin into the database that has a DateTime format.
The problem is the date it has, 12/31/1600 7:00:00 PM, is not correct and causes an error of
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
I've been checking the date format before it inserts it into the database.
If Not IsDate(empInfo.lastLogon) Then
empInfo.lastLogon = Format(Now.AddYears(-1), "MM/dd/yyyy HH:MM:ss")
End If
But doesn't seem to catch the year of 1600 which seems to be causing that error above.
Is there any code I can use to detect those weird years and replace it with a random, legit date so it will place it into the database?
I think your problem is caused because what is a valid date in the code is not a valid date in the database, probably because the database can store an earliest date of 1753 something, whereas the code allows much earlier dates (such as the 1600 you cited).
Try adding a test for the year to your IF statement:
If Not IsDate(empInfo.lastLogon) OrElse empInfo.lastLogon.year < 2000 Then
empInfo.lastLogon = Format(Now.AddYears(-1), "MM/dd/yyyy HH:MM:ss")
End If
Instead of using a default of 1 year ago, you might want to use a marker value like 01/01/2000 00:00:00. This will let you easily identify ones which didn't have a "real" value. But I don't know your business rules, so YMMV.
For months use MM, but for minutes use mm.
I.e.; you should use MM/dd/yyyy HH:mm:ss instead of MM/dd/yyyy HH:MM:ss.
I hope it helps you.
I think you want something like this:
If Not IsDate(empInfo.lastLogon) OrElse DateTime.Parse(empInfo.lastLogon).Year < 2000 Then
empInfo.lastLogon = Format(Now.AddYears(-1), "MM/dd/yyyy HH:mm:ss")
End If