timeformat in d3.js not working - d3.js

I am using the following code to get the hour and minute in d3.js
e.time=e.time.substring(12,19);
var timeformat=d3.time.format("%H:%M").parse;
e.time2=timeformat(e.time);
console.log(e.time2);
But I am getting null in output. My e.time contains following pattern of value
2015-03-29T20:32:24Z
e.time.substring(12,19) returns 20:32:24.
What is the mistake I am doing?

You need to do 2 things here:
Parse the string 20:32:24, to a date. For that use:
var timeformat=d3.time.format("%H:%M:%S").parse;
Format the Date, returning a string in the valid format )in this case hh:mm:
var hoursandminsformat=d3.time.format("%H:%M");
Fiddle here: http://jsfiddle.net/henbox/mwvvuazz/
A better approach, rather than using e.time.substring(12,19);, would be just to parse the full date object using:
var ISO8601format=d3.time.format("%Y-%m-%dT%H:%M:%SZ")
and then
var fomatted_time = hoursandminsformat(ISO8601format.parse("2015-03-29T20:32:24Z"));

Related

Can I use NSISO8601DateFormatter to parse an ISO-8601 string?

Foundation in macOS 10.12 has a new class, NSISO8601DateFormatter, which claims to be able to parse ISO-8601, but I'm having trouble understanding how to use it.
It takes a .formatOptions, which it says is used for parsing, too. Isn't the whole point of ISO-8601 that it's an unambiguous format?
For example, this works:
let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate]
f.date(from: "2018-11-26") // a Date
but this ignores the time:
let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate]
f.date(from: "2018-11-26T18:07:13Z") // a Date but wrong
If I add .withFullTime, this works, but then the first example fails.
How do I parse ISO-8601 with NSISO8601DateFormatter? Do I need to first pre-parse it on my own to figure out what sub-format of ISO-8601 it uses?

Validate Date FORMAT (not date string) using MomentJS?

I've seen that you can use an ".isValid()" function to check that a given string is in a date format:
moment('2007-05-05', 'YYYY-MM-DD', true).isValid()
But is there a way to confirm that the format is correct? For example:
'YYYY-MM-DD' should return true, but
'YYYY-MM-DDsadsadl' should return false since the characters at the end of the string aren't valid DateTime chars.
We're working on a tool that allows a user to input an existing date format, and then a second input to enter the desired format, but we need validation to ensure the string can properly parse and convert, but they aren't entering a specific date.
The application must accept any and all possible date formats.
Use the following function to validate your format.
validFormat = function(inputFormat){
var validation = moment(moment('2017-06-17').format(inputFormat), inputFormat).inspect();
if(validation.indexOf('invalid') < 0)
return true;
else
return false;
}
Do spend some time to understand this. This simply does a reverse verification using inspect(). The date 2017-06-17 can be replaced by any valid date.
This Moment Js Docs will help you identify the valid formats.
Just make a call to this function as
validFormat('YYYY MM DD')
const getIsValid = inputFormat => moment(moment().format(inputFormat), inputFormat).isValid()
Explanation:
moment().format(inputFormat) - Create a date string from the current time from that format
This is then wrapped with moment() to make that string a moment date object, defining the format to parse it with. Finally we call the isValid() property on that moment date object. This ensures we are able to both create and parse a moment with our custom format.

Correct strptime format for Input Type Time on Rails 4

Want to use the
<input name=attendance[something] type="time">
For time input. However cant make it seem to match with a Datetime object; for manipulation before insertion to ActiveRecord
myTimeIn = Time.strptime(params[attendance][something],"%H:%M")
keeps getting
invalid strptime format - `%H:%M'
What is the correct format for a input type=time field?
Looks like the value of params[attendance][something] may be blank or not of the correct format. You could do something like below to avoid the error:
t = params[attendance][something]
myTimeIn = Time.strptime(t,"%H:%M") if t =~ /\d{1,2}:\d{2}/
As per this HTML example, the value produced by <input/> of type time is HH:MM

convert date to format of the locale format type in android with xamarin with visual studio

How to get current format of the locale so that i can Format datetime with the format i get.
Means if i got the format then i can format that.
DateTime.Now.ToString(format);
This i am doing in visual studio with xamarin for creating Android application from C#.net.
Please help me for getting me the format.
Thanks & Regards
Parvez
You can use the culture info as formatter.
var d = DateTime.Now;
System.Diagnostics.Debug.WriteLine(
d.ToString(System.Globalization.CultureInfo.CurrentCulture));
System.Diagnostics.Debug.WriteLine(
d.ToString(System.Globalization.CultureInfo.CurrentUICulture));
try building the date format that you require, example below:
var now = DateTime.Now;
string newdateformat = string.Format("{0:00}/{1:00}/{2:0000}", now.Month, now.Day, now.Year);
If I understood your question correctly you want to format a DateTime to a string representation depending on the settings of the device (the locale).
You can do it like so:
var javaDate = new Date(YourDateTime.ToUnixTime()*1000);
var dt = DateFormat.DateTimeInstance;
SomeTextView.Text = string.Format("DateTime is: {0}", dt.Format(javaDate));
Notice that I used an extension method ToUnixTime() which converts DateTime to an Epoch time (also known as unix time) representation. You can find it easily even here on Stackoverflow.
(you can use DateFormat.DateInstance if you don't want the time part of the string)
Hope it helps.

Time scale won't convert dates to numbers

I am having trouble with using the time scale. I have dates in the format 'YYYYMMDD', I parse these with:
parseDate = d3.time.format("%Y%m%d").parse
I set the domain to static dates using the same above function. I can see the dates in correct format in the console. But when applying the scale function x, it returns 'NaN' WAT?
It's probably something small I'm not seeing, it's driving me mad...
Code can be found here: http://bl.ocks.org/pberden/5668581
I think the problem is in the way you call d3.nest. According to the spec
The key function will be invoked for each element in the input array, and must return a string identifier that is used to assign the element to its group.
You convert the Day in your csv file to a Date but then, as you are building a map from the array using d3.nest(), the invocation of the key function turns this Date to a String by doing an implicit conversion.
To fix this I think you could try to force your line generator to turn a String into Date like so
var line = d3.svg.line()
.x(function(d) { return x(new Date(d.key)); })
.y(function(d) { return y(d.values.Value); });

Resources