I'm looking for a JavaScript library which can parse and format date values and numbers. It should not be too big or complicated just 4 methods would suffice for me: parseDate, formatDate, parseNumber, formatNumber
//dt is a JavaScript date object, str a string containing a date, formatoptions specifies the format
var dt = parseDate(str, formatoptions);
var str = formatDate(dt, formatoptions);
//same applies for parseNumber and formatNumber just replace dt with nr which represents a JavaScript number
I've already asked Mr. Google but there are sooo many libraries and articles out there that it is hard to find something good. Many libs can format a number or date but can't parse them. Or they are not stable (reasonably bug-free).
Any recommendations?
update:
Sadly serverside processing is not really on option. I know support for date and time manipulation is much better in PHP or ASP
I included some sample code what I would expect from the parse and format functions
update 2:
I found quite a good library for formatting numbers which is called jquery-numberformatter
I have been using date.js from http://www.datejs.com. The parser in the home page will give you an idea of its capabilities.
These days I would look at MomentJs. Theres plenty of documentation and tests.
Related
how am I able to parse a time string in the following format 12:10AM and then get javascript to find the time difference between that time and the current time
Use momentjs. It can parse, manipulate, and display dates in javascript. It's incredible and has wide variety of parse formats and manipulations.
If you don't find the format available, just use Regex and extract the time components and pass them to momentjs. Then you can find the time difference and format the difference in displayable format also.
For more info on momentjs: http://momentjs.com/
You may use javascript regular expressions:
var dateParseRe = /(\d\d):(\d\d)(\w\w)/g;
dateParse.exec("12:10AM");
How do I return yesterday day in the format of yyyy-mm-dd using JavaScript and using
the regular expression below.
((19|20)\d\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])
Native JS date formatting is very primitive (see this question for more details).
If you can, a better solution is to use Moment.js to make working with dates in JS much, much better.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a standard date/time format that can be passed on a URL?
What is a good way for a RESTful resource to accept a datetime object? Specifically, I'm not sure what is a good way to represent the date and time as a query argument in the URL.
I was thinking of doing something like this:
GET /Calls?start=YYYY-MM-DD_HH:MM:SS
I'm using Javascript/jQuery on the Client and Python on the back end, so ideally it would be a format that could easily be written in Javascript and read in Python.
Thanks!
Use the ISO 8601 standard to encode the time argument as a string. It's readable for humans and supported by tons of libraries across many languages.
I'd recommend against using Unix time. Your sysadmins will thank you when they're asked to crawl or parse your web server logs for API calls. Using ISO 8601 will avoid them having to build a secondary step into that process to convert the Unix time number into something that actual humans have to understand.
Most datetime libraries (definitely both Python and JS) follow the same formatting approach, namely format strings. I'd go with any one that just uses digits, and uses descending order of size, i.e. YYYYMMDDhhmmss.
The one other thing to consider before jumping into a format is whether you might need to parameterize by something more akin to a date range, and if including the seconds, minutes, hours, etc. might over-specify the request and make it hard for the client to locate the data they are looking for.
unix time... http://en.wikipedia.org/wiki/Unix_time
why not just YYYYMMDDHHMMSS ?
As long as both sides can follow this format, I don't see any problem
I'm registering a javascript in aspx.net
Dim script = "<script language = javascript>" & _
"window.setTimeout('ShowTime(true, [?????])', 1000);</script>"
ClientScript.RegisterStartupScript(Me.GetType, "iniciar", script)
I must write a dateTime parameter in string format, but I'm not achieving the goal. I've tryed various time formats (Eg. 2011/02/10 17:05:00), without success. Ps: I know... when I try with only the date, its ok. But I need the time too. Thanks.
Javascript lacks a comprehensive string-based format mechanism. Aside from manually piecing the date together using the Date object's methods like getMonth and getFullYear, one would have to create a script to do such formatting, or use one of the myriad existing functions on the internet. Here is an excellent example: http://blog.stevenlevithan.com/archives/date-time-format
Date.js is a great library for all date-related function in Javascript.
Is there a working jQuery plugin (or a javascript 'library') for formatting datetimes? I found some, but they were:
not working with hours and minutes (the one from datapicker)
not fully functional - can't give you names of months, leading zeroes, etc.
are just a piece of code written in some blog.
Of course I can implement it, but it'd be better to reuse one. I seek functionality similar to Java's SimpleDateFormat
I've written a JavaScript implementation of the format() method of Java's SimpleDateFormat: http://www.timdown.co.uk/code/simpledateformat.php
The code is a few years old and I'd do it a bit differently now, but it's well tested and works.
Did you try date.js ?
It has a pattern recognition to format dates that is easy to use and has plenty of localisation files available.
ie: Date.today().toString("d-MMM-yyyy HH:mm")
I use http://blog.stevenlevithan.com/archives/date-time-format
Not sure whether it fits all your requirements, but this looks good one:
jquery-dateFormat