0

When I pass seconds to JS date object

var date = Date(seconds);

as a result I get

Tue Mar 25 2014 19:38:14 GMT+0100 (Middle Europe)

Is it possible to cut off some info that I don't need? For example I want output to be like

Tue Mar 25 2014 or
19:38:14

Additionally does there exists a function which is capable of converting English days and months into another language?

robert
  • 17
  • 7
  • 4
    [moment.js](http://momentjs.com/) – Andreas Mar 25 '14 at 18:44
  • possible duplicate of [Where can I find documentation on formatting a date in JavaScript](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – hsz Mar 25 '14 at 18:46
  • This might help for formatting the date the way you want. http://stackoverflow.com/a/1056730/3461196 – Sia Mar 25 '14 at 18:48
  • This does not work. When I do this: var i = Date(seconds); var x = i.getFullYear(); it returns an error: Object Tue Mar 25 2014 19:52:11 GMT+0100 (Střední Evropa (běžný čas)) has no method 'getFullYear – robert Mar 25 '14 at 18:54

3 Answers3

0

There is no such functions in JavaScript Date object. You have either to create your own or use some ready made like datejs

i100
  • 4,529
  • 1
  • 22
  • 20
0

Native JavaScript provides Date.toString() to convert a Date to a string like the one you showed, and also Date.toLocaleString() to convert a Date into a string according to configurations of the user's client.

For me, the best way to convert Dates to a readable string is to manually get the parts of the Date object you want to display. You may use date.getFullYear, date.getMonth, date.getDay or date.getDate. The main problem with this is that you get numbers for both Months and Dates, and you have to map the number to a string.

Note: Months start at 0, wich means Jannuray = 0, so for a classical mm/dd/aaaa format you need to add 1 to the value returned by date.getMonth.

A quick example on how to get a mm/dd/aaaa format out of a Date object:

var dateToString = (date.getMonth() + 1) + '/' + date.getDay() + '/' + date.getFullYear();

Another solution is to use a library such as datejs which makes it easy and simple.

user3417400
  • 341
  • 2
  • 4
  • I tried this already and when I do that I get an error. var storedDate = Date(seconds); var month = storedDate.getFullYear(); returns Object Tue Mar 25 2014 19:52:11 GMT+0100 (Střední Evropa (běžný čas)) has no method 'getFullYear. Any idea? – robert Mar 25 '14 at 19:08
  • Use the new operator when creating an object from a native Constructor function, var soredDate = new Date(seconds) – user3417400 Mar 25 '14 at 19:12
  • var x = Date(stored_seconds); returns me actual time var x = new Date(stored_seconds); returns me Sat Jan 17 1970. Whats that? :(( – robert Mar 25 '14 at 20:15
  • To get a Date object representing the current time use var x = new Date(); the native Date Constructor takes many combinations of parameters, visit http://www.w3schools.com/jsref/jsref_obj_date.asp to find more about the methods of an instance of the native Date object and the ways you can create those date objects with the Date constructor invoking it always with the new operator. – user3417400 Mar 25 '14 at 20:55
0

Try the jQuery Globalize...

By using jquery globalize you can localize the date into any other languages and also you can format the date as per your desired date format.

Soundar
  • 2,569
  • 1
  • 16
  • 24