/    Sign up×
Community /Pin to ProfileBookmark

The JavaScript client receives “2014-10-20 18:15:00” from the server. I would like to display it as “10/20/2014 6:15 PM”. I am using the Handlebars JavaScript template, and I know how to create helpers, so I think it is just a JavaScript question.

How is this best accomplished?

Thank you

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyOct 21.2014 — My solution is a little sloppy, but I'm designing it with simplicity in mind. The idea is to take [B][I]exactly[/I][/B] what you have said you will be getting as the date and convert that into the format you want. Honestly the easiest solution is to just format the date different when the server outputs it to the page, but I have to assume that this isn't possible based on your question.

I'll also note that it's possible to essentially do this same sort of thing by splitting up your date as a string and setting up a Date() object, but again I was aiming for simplicity.

[CODE]
var $dateFromServer = "2014-10-20 18:15:00"; // The date from the server goes in this string
var $d = new Date(Date.parse(String($dateFromServer.replace(" ", "T"))));
var $s = ($d.getHours() >= 12) ? 'PM' : 'AM';
var $h = ($d.getHours() + 11) % 12 + 1;
var $m = ($d.getMinutes < 10) ? "0" + $d.getMinutes() : $d.getMinutes();
var $newDateFormat = ($d.getMonth()+1) + "/" + $d.getDate() + "/" + $d.getFullYear() + " " + $h + ":" + $m + " " + $s;
console.log($newDateFormat); // Prints "10/20/2014 6:15 PM" out in the console
[/CODE]
Copy linkTweet thisAlerts:
@rootOct 22.2014 — The JavaScript client receives "2014-10-20 18:15:00" from the server. I would like to display it as "10/20/2014 6:15 PM". I am using the Handlebars JavaScript template, and I know how to create helpers, so I think it is just a JavaScript question.

How is this best accomplished?

Thank you[/QUOTE]


Whats wrong with getting the serverside to issue the date and time in a format thats going to be useful?
Copy linkTweet thisAlerts:
@NotionCommotionauthorOct 22.2014 — Thank you Sup3rkirby and .,

Yes, I can format at the server. The thing is I get mixed messages. Some say "the view should perform all formatting, not the model!!!!"

Maybe I shouldn't care?
Copy linkTweet thisAlerts:
@rootOct 22.2014 — You do whats easiest.
Copy linkTweet thisAlerts:
@rootOct 22.2014 — If you really have to, then you can already find ready made functions, example of a function that emulates the PHP date() function. http://phpjs.org/functions/date/
Copy linkTweet thisAlerts:
@NotionCommotionauthorOct 23.2014 — Thanks ..

I also found https://github.com/phstc/jquery-dateFormat and http://momentjs.com/. momentjs seems the most popular, but don't know if it is overkill.

Any experience with either?
Copy linkTweet thisAlerts:
@deathshadowOct 23.2014 — The thing is I get mixed messages. Some say "the view should perform all formatting, not the model!!!![/QUOTE]
If you have "view" in that stupid malfing "MVC" model being done clientside with scripting, you're probably doing things wrong. Since your server-side code is likely outputting HTML and text, that's part of the view unless you've gone and done something stupid like pissing on accessibility with AJAX and/or providing no graceful degradation.

Generally speaking, I'm [b]NOT[/b] convinced MVC has any business being done with web technologies, because it's not how things like server-side languages or web communication works, nor should it be. I'm all in favor of separating certain things server-side and client-side; like output from processing -- but true MVC?

Sure, it's brilliant in event driven environments like accessing a WM API from languages like C++ or JAVA... but for things like PHP or ASP where you are recieving requests and flat processing them to create markup? That's taking a deuce and a half lump and using it to pound that square peg into a round hole.

Though yeah, you'll have a lot of newly graduated and recent to the field halfwits blindly believing that MVC is great just because they were told so by other nimrods likely not qualified to be opening their mouths on the subject. [i]Sadly, that's true about a LOT of web technologies and how people are using/abusing them ? [/i]
Copy linkTweet thisAlerts:
@NotionCommotionauthorOct 23.2014 — If you have "view" in that stupid malfing "MVC" model being done clientside with scripting, you're probably doing things wrong. Since your server-side code is likely outputting HTML and text, that's part of the view unless you've gone and done something stupid like pissing on accessibility with AJAX and/or providing no graceful degradation.

Generally speaking, I'm [b]NOT[/b] convinced MVC has any business being done with web technologies, because it's not how things like server-side languages or web communication works, nor should it be. I'm all in favor of separating certain things server-side and client-side; like output from processing -- but true MVC?

Sure, it's brilliant in event driven environments like accessing a WM API from languages like C++ or JAVA... but for things like PHP or ASP where you are recieving requests and flat processing them to create markup? That's taking a deuce and a half lump and using it to pound that square peg into a round hole.

Though yeah, you'll have a lot of newly graduated and recent to the field halfwits blindly believing that MVC is great just because they were told so by other nimrods likely not qualified to be opening their mouths on the subject. [i]Sadly, that's true about a LOT of web technologies and how people are using/abusing them ? [/i][/QUOTE]


I think a basic MVC works pretty well. I've found a template engine like Twig helps, albeit probably slows things down a bit. Controller does a bit of logic, requests data from the model, and initiates the view. Doesn't even rely on client script, and thus has no degrading issues.

I am also not against pissing on accessibility on certain cases. Why not use Ajax for a highly interactive application which caters to a limited audience? Doing differently will either reduce the UI or increase development costs, and the benefits will not exceed the costs. How I currently implement this is have the server view create the main page HTML. The server is also responsible to provide JSON upon an Ajax request. I still am, however, struggling where to draw the line between different parts of the application.

I am also considering using angularjs for future projects, however, I currently know very little about it.
Copy linkTweet thisAlerts:
@deathshadowOct 23.2014 — ... meanwhile you listed a bunch of things I'd never put on a website in the first place.

I've found a template engine like Twig helps[/quote]
As a "hypertext preprocessor" PHP already IS a template engine -- so you're running a template engine atop a template engine? Pointless garbage code bloat to replicate existing functionality... Twig, Smarty -- it's all a bunch of extra garbage to sweep developer ineptitude under the rug.

I am also not against pissing on accessibility on certain cases.[/quote]
Where I am in most every case against it -- narrowing the target audience is shortsighted, ignorant, and could even get you fined in certain nations with certain clients. (like the UK).

... though I'm not saying you can't use AJAX, but for **** sake code a scripting off fallback. If you can't implement the functionality without scripting FIRST, it probably has ZERO business being done on a website in the first place. GOOD scripting enhances functionality, instead of supplanting it.

To me, all that extra code for nothing, bandwidth wasting bull and pointless needless complexity is just going to COST you users, take longer to develop and cost more in both the long and short-term. I often sit here wondering just what the devil is in the kool-aid when people say it's the other way around... ON WHAT PLANET?

Only way that would make sense is if you're not qualified to write the code and instead just slap together off the shelf solutions any-old-way. Though that does seem to be the target audience of "Frameworks" -- be it server-side or worse, halfwit clientside bull like bootcrap and jqueery... or as you just mentioned, angularjs.

Really you might want to back up a bit and take a good hard look at what you are doing, and adopt this mantra: "If you can't do it without javascript first, you probably have no business doing it at all." -- I would even extend that to pointless code bloat garbage like server-side frameworks; if it's complex enough that using them is providing a real benefit, you're probably doing something wrong.

Like pissing off users and not even being aware of doing so; or if you are aware of it you don't know why it's a problem. See 99%+ of the people who come to sites like this asking what's wrong with their turdpress+bootcrap train wreck using megabytes of code to deliver single digit K totals of actual content. The real laugh being most often the first few responses will be dip****s telling them to throw more code at it.

But as I said, I agree with the separation, just not that the MVC paradigm has any business being implemented in a language like PHP because it's [b]NOT[/b] an event driven environment. The approach I like has no real name, but basically you have a index.php that acts as a traffic cop, directing requests to the appropriate sub-file which then generates the data/processes the request, which then hands off to the template for output. The difference is instead of MVC's out-of-sequence event driven nature, it's flat top-down execution -- what PHP and web requests actually ARE!

[i]edit -- woah, I just used the word paradigm... Usually I make fun of that Simpsons-style![/i]
×

Success!

Help @NotionCommotion spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...