I want method to convert string 24 hours time format to 12 format
For example the method takes string 16:30
Then return string 4:30 pm
The intl package do not work because the package needs the data and time but I'm reading the time from Jason file without date
as an option
String convertTo12(String hhMM) {
final arr = hhMM.split(':');
final h = int.tryParse(arr[0]);
return '${h > 12 ? h % 12 : h}:${arr[1]}';
}
final str = '16:30';
print(convertTo12(str)); // 4:30
Related
I am trying to convert 12 digit c# date time ticks formatted time. (648000000000). With the help of following link I added an extension to my code How to get 18-digit current timestamp in Swift?.
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
let date = Date(ticks: 648000000000)
When I try to see result date it prints following;
0001-01-03 18:00:00 +0000
However, when I try to convert it hour and minute format output is irrelevant like 19:55
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.string(from: date)
My first question is how can I format it to get only 18:00. Second is why it is printing 18:00 when I print only date, but 19:55 when I format it?
Just don't subtract 62_135_596_800
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000)
}
}
1970-01-01 18:00:00 +0000
The other problem: When you create date and print it, the string is formatted in UTC time zone (offset GMT+0). But DateFormatter returns string representation dependent on its time zone, which is the local timezone by default.
You can fix your code just by setting dateFormatter's timeZone to UTC
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
18:00
Apparently your timestamp represents a duration as the number of 100-nanosecond ticks, not a date. If you divide the number by 10^7 then you get the number of seconds. These can be printed as a duration with a DateComponentsFormatter.
Example:
let ticks = 648000000000
let seconds = TimeInterval(ticks) / 10_000_000
let fmt = DateComponentsFormatter()
fmt.allowedUnits = [.hour, .minute]
print(fmt.string(from: seconds)!)
18:00
The duration is 64800 = 18 * 60 * 60 seconds, that are exactly 18 hours.
I am calling a legacy service that returns all times in a string format assuming that the timezone is EST. How do I create a DateTime instance and specify that the timezone is EST? When I display the times to the user's I will translate to their device timezone.
I wrote a package for this. It's called Instant, and it can fetch the latest DateTime in any given zone worldwide. Take a detailed look at https://aditya-kishore.gitbook.io/instant/
The basic usage to get a DateTime in a timezone is fairly simple.
Note: I assume below that your legacy service delivers a String with format HH:MM in 24 hr time, and that it doesn't deliver a date. If any of the following is untrue, lemme know and I'll change the code sample.
List<int> _hourmin (String parse) {
List<int> timeinint = [];
if (parse.length == 5) {
//if parse is something like 10:00
timeinint.add(int.parse(parse.substring(0,2))); //Add hours (10)
timeinint.add(int.parse(parse.substring(3,5))); //Add minutes (0)
} //hours >= 10
if (parse.length == 4) {
//if parse is something like 4:06
timeinint.add(int.parse(parse.substring(0,1))); //Add hours (4)
timeinint.add(int.parse(parse.substring(2,4))); //Add minutes (6)
} //hours < 10
return timeinint;
}
DateTime utcDateTimeFromZone(
{#required String zone,
int year = 0,
int month = 0,
int day = 0,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0}) {
hour = (hour - timeZoneOffsets[zone].truncate()) % 24; //Convert to UTC from timezone
if (hour<0) {
hour = 24 + hour;
}
minute = (minute - ((timeZoneOffsets[zone]%1)*60).round()) % 60;
if (minute<0) {
minute = 60 + minute;
}
//Convert to UTC from timezone
DateTime inUTC = DateTime.utc(year, month, day, hour, minute, second, millisecond, microsecond); //Initialize in UTC time
return inUTC;
}
//We get an array containing the hours minutes
List<int> time = _hourmin("4:25"); //put your API call as the param here
//This is what you need
DateTime finished = utcDateTimeFromZone(zone: "EST", hour: time[0], minute: time[1]);
print(formatTime(time: finished.toLocal())); //1:25 for me, because I'm PST, but this works with any local timezone
Hope that clears it up!
The included DateTime class only supports the local timezone or UTC but no specific timezone.
https://pub.dartlang.org/packages/timezone would provide that but it looks like it needs an update for Dart 2.0.0
https://pub.dartlang.org/packages/time_machine is a newer one.
I am using this following way while doing an export to excel
alasql.fn.Date = Date;
alasql('SELECT new Date(mydateString) AS CUSTOM_DATE INTO XLS("' + filename + '.xls",{headers:true}) FROM ?', [items]);
Those above lines are printing in this below date format
Sat Jun 06 2015 00:00:00 GMT-0500 (Central Daylight Time)
but I want to format mydateString to this below format
MM-DD-YYYY hh:mm:ss
How can I do that?
I did this below instead and create my own format for date
alasql.fn.datetime = function(dateStr) {
var date = new Date(dateStr);
return date.toLocaleString();
};
alasql('SELECT datetime(mydateString) AS CUSTOM_DATE INTO XLS("' + filename + '.xls",{headers:true}) FROM ?', [items]);
I have String time="02:30 PM" means 12 hour format and i want to convert this time in 24 hour format .
I want this o/p: 14:30. so how can i convert this ?
I don't know anything about berries, but in case the API is missing a proper formatting function, you can always get your hands dirty with the string itself:
static String convert(String time){
boolean pm = "PM".equals(time.substring(6).toUpperCase());
int h = Integer.valueOf(time.substring(0,2));
if (h!=12)
h+=pm?12:0;
else
h=pm?h:0;
return ((h<10)?"0":"")+h + ":" + time.substring(3,5);
}
Try this code.
This will give the current time in 24 Hours format.
SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
String newTime = formate.formatLocal(System.currentTimeMillis());
I want to convert todays date into the julian date format . Suppose if the date is 31/12/2011(31st dec 2011) , then the julian date should be 11365(yyddd) .
Any help would be much appreciated
Thanks
One simple way to get the date in yyddd format would be doing some mathematics:
date theDate = 31\12\2011;
int theOrdDate;
;
theOrdDate = 1000 * (year(theDate) mod 100) + dayOfYr(theDate);
// or
theOrdDate = 1000 * (year(theDate) - 2000) + dayOfYr(theDate);
EDIT: one possible drawback: this calculation will return only 4 digits if the year is less than 2010.
In AX2012 this task is super easy.
str myJulian;
myJulian = date2Julian(today());