Skip to content

Instantly share code, notes, and snippets.

@hisamafahri
Last active May 29, 2022 10:11
Show Gist options
  • Save hisamafahri/332d6b8d650baf372b7179c0051febfe to your computer and use it in GitHub Desktop.
Save hisamafahri/332d6b8d650baf372b7179c0051febfe to your computer and use it in GitHub Desktop.
void main() {
dateTimeConverter();
}
//The codes below is the manual way to change it. (I know it is not effective)
DateTime dateTimeConverter (){
String month;
String myDate = 'Tue, 28 Jul 2020 22:00:00 +0900'; //RSS PubDate source. I assume it will be constant
String hour = myDate.substring(17, 25); //Get the hour section [22:00:00]
String day = myDate.substring(5, 7); //Get the day section [28]
if(myDate.substring(8, 11) == 'Jan'){ //Converting the month Section
month = '01';
} else if(myDate.substring(8, 11) == 'Feb'){
month = '02';
} else if(myDate.substring(8, 11) == 'Mar'){
month = '03';
} else if(myDate.substring(8, 11) == 'Apr'){
month = '04';
} else if(myDate.substring(8, 11) == 'May'){
month = '05';
} else if(myDate.substring(8, 11) == 'Jun'){
month = '06';
} else if(myDate.substring(8, 11) == 'Jul'){
month = '07';
} else if(myDate.substring(8, 11) == 'Aug'){
month = '08';
} else if(myDate.substring(8, 11) == 'Sep'){
month = '09';
} else if(myDate.substring(8, 11) == 'Oct'){
month = '10';
} else if(myDate.substring(8, 11) == 'Nov'){
month = '11';
} else if(myDate.substring(8, 11) == 'Dec'){
month = '12';
}
String year = myDate.substring(12, 16); //Get the year section
String date = year + '-' + month + '-' + day + ' ' + hour; //Combine them
print('Before Parsing: $date');
DateTime parsedDate = DateTime.parse(date); //parsed it.
print('After Parsing: $parsedDate');
return parsedDate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment