Javascript time string to date - javascript

I have a time as a string like "10:00pm" and I need to convert it into a date object so it can be represented in an input type="time" tag. Im using angularjs to model it.
I get an error saying "Expected 10:00am to be a date". How can I convert "10:00am" into a date object? I haven't been able to figure it out. Any help is appreciated.
I only need the time

Have you seen http://momentjs.com/ ?
You can use it like this:
var time = moment("10:00pm", "HH:mm a");

If you're using date.js then you could try
Date.parseExact("10:00 PM", "hh:mm tt");
This should also pick up if you've loaded the library correctly.

Related

How to convert date format in JavaScript?

In my Rails application, in JSON responses from controller, dates are coming as 2015-06-22T18:08:22+05:30. I want to convert the date into 22-06-2015, 06:08 PM format in Javascript. I checked may resources in Stackoverflow but none is working. Is there any way to do that?
you can use date.format library
Here's a example fiddle
var date = new Date("2015-06-22T18:08:22+05:30");
var dateString = date.format("dd-mm-yyyy hh:MM:ss TT Z");
alert(dateString);
Also it would be better to format it on server side if you dont want to include extra libraries
moment is a very good library for dealing with date.
console.log(moment('2015-06-22T18:08:22+05:30').format('DD-MM-YYYY h:mm:ss a'));
> 22-06-2015 7:38:22 pm
There are Two Ways:
Do the conversion in the Rails and send the value via JSON (using strftime or u can use Date.strptime or Time.strptime)
Code:
User.created_at.strftime("%d/%m/%Y")
Doing the conversion in Front End using Moment Js:
Code:
console.log(moment('2015-06-22T18:08:22+05:30').format('DD-MM-YYYY h:mm:ss a'));

correct time in date object in Angular js

I'm going crazy with this simple stuff:
$scope.targetData=new Date("09 21 2015 18:04:00");
$scope.clock = new Date();
This render : targetData="2015-09-21T16:04:00.000Z" and clock="2015-09-21T16:36:53.314Z"
but in Italy it is 18:04:00... so how can I set correct ??
Thanks..
If you are willing to use dependencies, an extermely easy and well-used solution for almost all Date related usages and problems, moment.js can be your friend:
http://momentjs.com/
http://momentjs.com/timezone/
Angular-Ported:
https://github.com/urish/angular-moment
First of all, it has nothing to do with AngularJS. Date is JavaScript object.
I would say, that it simply converts input time to your local time. And you're saying, that the input time is in UTC. Try to add your timezone explicitly. That means instead of "Z" (means +0000 which is UTC) as a timezone, use your timezone or just try to omit timezone completely, I guess it will expect input time is in your local time.
See Date documentation for details and date time format.

Javascript Date conversion Invalid Date

I was looking for this very specific conversion which I couldnt find anywhere
var d = new Date("2014-12-25T18:30:00+0100");
console.log(d.toString());
the console.log returns an "Invalid Date"
The DateString is returned by the Facebook GraphAPI.
What am I doing wrong? can anyone help?
Thanks in advance
EDIT:
Now that I fixed the API my output is kind of consfusing:
I tried splitting up the String
d.getDay()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes();
it outputs
4.11.114 18:30
why?!
Instead of doing those complicated date functions
d.getDate()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes();
Do yourself a favour and include http://momentjs.com/ in your project. You can then simply take the date from the facebook api and format it with
moment("2014-12-25T18:30:00+0100").format("/* date format */");
See here for formating
SIDENOTE
When formating dates in plain javascript, you will have to add 1 month to your month - january is 0, that's why you get 4.11... instead of 4.12...
Change getYear() to getFullYear()
d.getDay()+'.'+d.getMonth()+'.'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes();

Angularjs date display

Hi I am trying to figure out how to display date properly using angularjs.
My system returns the date in this fromat
c_date="\/Date(1151470800000-0500)\/"
I dont know what format is it.
I am displaying it
using <span>{{pro.c_date | date:'medium')</span>
When i run this i get date printed in same format
"/Date(1151470800000-0500)/"
Can anyone suggest how to fix this so date can be displayed properly.
Thanks
the date that you're trying to use is a invalid date or maybe I don't know what format are the system using.
The problem is that angular.js requires a data object for can format it. Not a date string.
I suppose that the date is 1151470800000 miliseconds since the epoch least 0500 for adjust hours or something.
If this is correct, you only need to make c_date a Date object before pass to view.
c_date = "\/Date(1151470800000-0500)\/";
var the_date = eval("new "+c_date.replace(/\\|\//g, ''));
http://jsbin.com/zuwizoxe/3/
Regards!

Convert a time string say '12:05 PM' into a datetime using Date.Parse in Javascript

I want to convert a time string say '12:05 PM' into a datetime using Date.Parse in Javascript.
When I pass in a value of say 12:05 PM or 12:10 PM or ... or 12:55 PM the value returned by startTime below is null, i.e. startTime = null
But when I pass in values of 1:00 PM, 1:05 PM, 1:10 PM, 12:00 AM,...,12:00 PM it works fine
returning me a Date with the time included.
This is the code line causing an issue:
var startTime = Date.parse($("#<%= StartTime.ClientID %>").val()); //code causing the issue
And StartTime is a textbox.
I am writing the above code in client/html in an ASP.NET application on the web form.
If you're using date.js then try (as per test case here)
Date.parseExact("12:05 PM", "hh:mm tt");
This should also pick up if you've loaded the library correctly.
It works fine here:
http://jsfiddle.net/vuURb/396/
It's possible that it is a library loading issue, but you claim it works on some times and not others. Have you tried outputting the value of the text box to the console before feeding it to Date.parse()?
there's a good utility for dates named date.js.
Based on this answer you can do this:
var startTime = new Date();
var time = $("#<%= StartTime.ClientID %>").val().match(/(\d+)(?::(\d\d))?\s*(p?)/);
startTime.setHours(parseInt(time[1]) + (time[3] ? 12 : 0) );
startTime.setMinutes( parseInt(time[2]) || 0 );
I just read on your reply to the other question that you are using date.js. If you really are using it, your code is correct, then the problem should be that you are not loading the library properly, and you are using the native Date object.

Categories