How Long has Obama Been a Senator?

Barack Obama had only been a United States senator for a period of four years before being elected president. He was however a state senator in Illinois previously.