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");
Related
I have this weird format of dates 1640828010231 which I'm trying to convert to actual date using javascript. I'm using the following code.
new Date('1640828010231')
but it is giving me the current time. I have no idea what format that date is in. I have tried various ways. please help.
It's timestamp,
Have a look to mozilla doc to understand better https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_ecmascript_epoch_and_timestamps
I have had a truly gnarly time trying to work with Date objects in javascript, which strike me as ugly and unintuitive.
I am getting back two objects from an internal API: one date string in the form of YYYY-MM-DD and one time string in the form of HH:MM.
What I want to do, in javascript, is to merge these into a UTC-formatted string of the form YYYY-MM-DDTHH:MM:00Z. I have been playing around a bit with the moment.js and moment timezone libraries, but I'm not sure how I might leverage these to complete this task.
I solved this thusly using moment timezone:
dateTime = date.concat(" ").time
dateTimeAdjusted = moment.tz(dateTime, timezone).format()
(where timezone was a variable that I had access to that contained the timezone of the given date and time.)
I have a DateTime string which I can assume is in a parsable format using Date.parse() or a 3rd party library. The format could differ based on a user's location and settings. For example, here are some strings I could see: "2014/08/19 16:10:20" or "08/19/2014 4:10:20 PM".
I would like to manipulate the time, say by adding 30 minutes, but also preserve the original format string when I present the new time to the user. I suppose I'm looking for something like a parse function that gives you a DateTime along with the format string it detected. Is there an easy way to accomplish this? 3rd party libraries are welcome, with a preference towards jQuery since I'm already using that.
Note: I don't know what the format is going to be before I parse the datetime string. It can be one of many different formats. (However I probably could build a list of possible format strings.)
I need to display the date format like this 15-Apr-2012 10.12 AM.I tried to use time=new Date() using javascript.But i couldnt get the correct date format .it shows the GMT global timing.I need to display the time format.please anyone can know?
Take a look at DateJS. If you're planning to do more stuff with dates than just formatting it, I would strongly suggest it. Definitely makes things easier with manipulation and displaying.
Easiest thing to do is use a library: try Moment, it is the real deal.
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.