sorting based on date and time in flex - sorting

here i used the sort function for date and time,but it's only sort on date not time,i want sort based on both date and time:
private function dregdate_sortCompareFuction(itemA:Object, itemB:Object):int {
var dt1:Date = DateField.stringToDate(String(itemA.sbatchregdate),"DD/MM/YYYY");
var dt2:Date = DateField.stringToDate(String(itemB.sbatchregdate),"DD/MM/YYYY");
var dateA:Date = new Date(dt1);
var dateB:Date = new Date(dt2);
return ObjectUtil.dateCompare(dateA, dateB);}
kindly please share your knowledge.
thanks

As you can see by the format you're passing, DateField.stringToDate only cares about dates and not times.
You could use Date.parse if your input string was in one of these formats:
MM/DD/YYYY HH:MM:SS TZD
HH:MM:SS TZD Day Mon/DD/
Mon DD YYYY HH:MM:SS
Day Mon DD HH:MM:SS TZD YYYY
Day DD Mon HH:MM:SS TZD YYYY
Mon/DD/YYYY HH:MM:SS TZD
YYYY/MM/DD HH:MM:SS TZD
but yours is not.
So give this, i'd use a Regular Expression to extract the individual values from the string and then build the Date object manually, something like:
var regex:RegExp = /^(\d+)\/(\d+)\/(\d+) (\d+):(\d+)$/i;
var data:Object = regex.exec(itemA.sbatchregdate);
var dateA:Date = new Date(o[3], o[2]-1, o[1], o[4], o[5]);
once you have the two dates created with the proper time values, the comparison should work as expected.

Related

NSDateFormatter have extra dot in front of month for nb_NO (Norsk Bokmal) locale

I'm working in a Xamarin.IOs project. I'm trying to convert a DateTime object into the following format "dd. MMM yyyy" under "nb_NO" locale which should return something like, Ex: "05. feb 2018".
I'm using the following code snippets to achieve this, (C#)
var now = DateTime.Now;
var format = "dd. MMM yyyy";
var formatter = new NSDateFormatter { DateFormat = format, Locale = new NSLocale("nb_NO"), TimeZone = NSTimeZone.SystemTimeZone };
var nsDate = DateTimeToNSDate(now);
var formattedDate = formatter.StringFor(nsDate);
But what I get for formattedDate is something like Ex: "05. feb. 2018". There is an extra dot (period) after the month as well.
What can I do to get rid of this unexpected extra dot after the month in "nb_NO" locale?
After a long research, I tried the following string format, "dd. LLL yyyy".
You have to use LLL instead of MMM to render the month.
This just solved me the issue.
You can find all the possible formats here!

Swift DateFormatter Extract Time

I've been trying to return a date with just Time. I tried DateFormatter, but will always retrieve a full date for some reason.
//Declare Date Formatter 1
let dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "yyyy-MM-dd HH:mm:ss"
//Declare Date Formatter 2
let dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = "hh:mm"
//Retrieve date and set to proper date for DateFormatter
var date: Date = dateFormatter1.date(from: "2017-11-28 10:47:30")!
//Set String date to time format with dateFormatter2
let dateString = dateFormatter2.string(from: item)
//Reseting dateFormatter1 for to only use time (could be ambiguous)
dateFormatter1.dateFormat = "hh:mm"
//Set date to Date
date = dateFormatter1.date(from: dateString)!
print("String: \(dateString)")
print("Date: \(date)")
Output:
String: 10:47
Date: 2000-01-01 15:47:00 +0000
- timeIntervalSinceReferenceDate : -31565580.0
I want the Date: to be 10:47
Is this even possible?
No, it is not possible. The Variable date is of type Date which has full date information within it. When showing it in string format you can convert it as you want, but while storing in the variable of type Date it stores the full information.

How to find date lies in which week of month

Suppose I have a date in year-month-day format. Say "2015-02-12". Now I want to find that in which week this date lies. I mean 12 lies in 2nd week of Funerary. I want if I fo something like
LocalDate date = 2015-02-12;
date.getWeekOfMoth should gives me 2 because 2 lies in 2nd week of February. How can i do it ?
Thanks
Edit
Hi, I am so sorry. I should replied you before you asked. I tried with the following code
String input = "2015-01-31";
SimpleDateFormat df = new SimpleDateFormat("w");
Date date = df.parse(input);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_MONTH);
System.out.println(week);
It prints 2.
While when I check with the following code
String valuee="2015-01-31";
Date currentDate =new SimpleDateFormat("yyyy-MM-dd").parse(valuee);
System.out.println(new SimpleDateFormat("w").format(currentDate));
It prints 5.
Try this one. Remember to feed it with your date format and string with this date as input.
SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(input);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_MONTH);

Simple D3: turn a date object into an attractive string?

This should be easy!
I have a JavaScript Date object and I want to turn it into an attractive string, using D3's time functions.
var format = d3.time.format('%e %b').parse;
console.log('graph_x', graph_x, typeof graph_x, String(graph_x), format(String(graph_x)));
This gives me:
graph_x
Tue Dec 25 2012 08:26:10 GMT+0000 (GMT)
object
Tue Dec 25 2012 08:26:10 GMT+0000 (GMT)
null
How can I turn my Date object into a nice String, like "Tue 25"?
d3.time.format takes a date object and returns a string.
format.parse takes a string and returns a date time object.
From the docs:
var format = d3.time.format("%Y-%m-%d");
format.parse("2011-01-01"); // returns a Date
format(new Date(2011, 0, 1)); // returns a string
Since you already have a date object, you don't need to parse it:
var format = d3.time.format('%e %b')
console.log(format(graph_x))

Date format in D3.js

I have a column (date) in csv which stores the date in "2003-02-01"(y-m-d). I would like to format the date in month and year like Apr 2003. how do i do that?
var format = d3.time.format("%m-%Y");
data.forEach(function(d,i) {
d.date = format(d.date);
});
I am getting the following error Error: TypeError: n.getFullYear is not a function Line: 5
the csv file contains values:
200,300,400,288,123,2003-01-01
300,700,600,388,500,2003-02-01
what is the issue here?
Javascript doesn't automatically recognize the values in the CSV file as dates, just reading them in as strings. d3's time functions make it pretty easy to convert them to datetime objects:
> parseDate = d3.time.format("%Y-%m-%d").parse
> parseDate('2003-01-01')
Wed Jan 01 2003 00:00:00 GMT-0600 (Central Standard Time)
To format the dates like you want, we need to go the other way, from a date object to a string:
> formatDate = d3.time.format("%b-%Y")
> formatDate(parseDate('2003-01-01'))
"Jan-2003"
I would recommend representing your dates within your program with date objects and only formatting them as strings when you need to display them.
D3 version 4 has different time methods now.
https://keithpblog.org/post/upgrading-d3-from-v3-to-v4/
https://github.com/d3/d3/blob/master/CHANGES.md
Your source date
var d = {created_time : "2018-01-15T12:37:30+0000"}
The structure of that date = %Y-%m-%dT%H:%M:%S%Z
I search google with "2018-01-15T12:37:30+0000" and it's suggestions provided the date structure string. Handy.
Create a timeParser to convert(parse) your date string into a date object
var parseDate = d3.utcParse("%Y-%m-%dT%H:%M:%S%Z")
//parseDate(d.created_time)
Now create a time formatter to return a formated date string for displaying.
var formatDate = d3.timeFormat("%b-%Y")
return formatDate(parseDate(d.created_time))
i.e.
Jan-1970
from Adam answer, here is a small helper function to convert a time string from a format to another:
var formatTime = function(input, formatInput, formatOutput){
var dateParse = d3.time.format(formatInput).parse;
var dateFormat = d3.time.format(formatOutput);
return dateFormat(dateParse(input));
};
use:
formatTime("2003-01-01", "%Y-%m-%d", "%b-%Y");
// output -> "Jan-2003"
if you are getting date lets say in variable " d = (e.g: '2003-03-01')" in string format then,
var monthNameFormat = d3.time.format("%b-%Y");
return monthNameFormat(new Date(d));
this will result date in "Jan-2003" format.
In D3Js v3, this worked for me:
var s = "2018-11-01T19:37:55Z";
d3.time.format("%Y-%m-%dT%H:%M:%SZ").parse(s);
// Thu Nov 01 2018 19:37:55 GMT-0700 (Pacific Daylight Time)

Resources