How can I obtain the current time in HH:MM:SS am/pm format?
There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM. aa – AM/PM marker. In this example we are displaying current date and time with AM/PM marker.
Get time format of datetime in sql. ORDER BY expr1 ; SUBSTRING(CONVERT(varchar(20), yourdate, 22), 18, 3)-->it gives you a am/pm.
To make formatting a date-time to AM/PM format easier, we can use the toLocaleString method. For instance, we can write: const str = new Date(2021, 1, 1). toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }) console.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"hh:mm:ss a"]; NSLog(@"Current Date: %@", [formatter stringFromDate:[NSDate date]]); [formatter release];
The format of the string in setDateFormat
is based on ISO-8601, http://en.wikipedia.org/wiki/ISO_8601#Dates
Swift 3 Version:
let formatter = DateFormatter() formatter.dateFormat = "hh:mm:ss a" print("Current date: \(formatter.string(from: Date()))") // -> Current date: 08:48:48 PM
There's also a new site since this original answer that helps writing these format strings. Aptly named... http://nsdateformatter.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With