Guest User

Untitled

a guest
Oct 16th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. ## Time formatting snippets
  2.  
  3. ### Java
  4.  
  5. ```java
  6. import java.util.Date;
  7. import java.text.DateFormat;
  8. import java.text.SimpleDateFormat;
  9.  
  10. private String getTime() {
  11. DateFormat dateFormat = new SimpleDateFormat("[dd.MM.yyyy - HH:mm:ss]");
  12. return dateFormat.format(new Date());
  13. }
  14. ```
  15.  
  16. ### Python
  17.  
  18. ```python
  19. from time import gmtime, strftime
  20.  
  21. def get_time():
  22. return strftime("[%d.%m.%Y - %H:%M:%S]", gmtime())
  23. ```
  24.  
  25. ### JavaScript
  26.  
  27. ```javascript
  28. // That method is verry rough. Is there a better way?
  29.  
  30. function getTime() {
  31. function btf(inp) {
  32. if (inp < 10)
  33. return "0" + inp;
  34. return inp;
  35. }
  36. var date = new Date(),
  37. y = date.getFullYear(),
  38. m = btf(date.getMonth()),
  39. d = btf(date.getDate()),
  40. h = btf(date.getHours()),
  41. min = btf(date.getMinutes()),
  42. s = btf(date.getSeconds());
  43. return "[" + d + "." + m + "." + y + " - " + h + ":" + m + ":" + s + "]" ;
  44. }
  45. ```
  46.  
  47. ### C#
  48.  
  49. ```c#
  50. private string GetTime() {
  51. return DateTime.Now.ToString("[dd.MM.yyyy - HH:mm:ss]");
  52. }
  53. ```
  54.  
  55. *Write some other time formating functions for other languages in the comments below and I will add them to the snippet ;)*
Add Comment
Please, Sign In to add comment