Hi there seems to be plenty out there to convert a US formatted date (MM/DD/YYYY) to a UK date (DD/MM/YYYY), but I'm trying to do the opposite: i receive a load of UK dates (DD-MM-YYYY) from the server which I'm trying to format as DD-MMM-YYYY (eg: 11 Jan 2013 ), but Flex thinks it's trying to convert American days and so when it gets 17-02-2013, it returns an empty string because there is no month 17.
How do i tell it I'm giving it UK dates and not US dates?
Thanks in advance.
Short answer, you can't.
Longer answer, there's not enough information to determine the date format. As you saw, in some cases you CAN determine that the date is not valid for the expected format (17-02-2013 - since 17 isn't a valid month, it must be DD-MM-YYYY rather than MM-DD-YYYY), but for just under half of the dates you just can't tell (is 01-02-2013 supposed to be Jan 2 or Feb 1?)
If you DO know that the dates are supposed to be in a particular format, you can use DateField.stringToDate to parse the string:
var value:Date = DateField.stringToDate(dateString, 'DD-MM-YYYY');
OK, got round it in the end with this ugly hack: ended up converting the string into an array and re-organising it so that it would fit the american format before using the DateFormatter.format on the result. There must be a more elegant way...
<Script>
public convertDateStringToUS( date : String ) : String
{
var dateArray : Array = date.split( "-" );
var day = dateArray[0];
var month = dateArray[1];
var year = dateArray[2];
date = month + "-" + day + "-" + year;
return date
}
</Script>
<declarations>
<mx:DateFormatter id="dateFormatter"
formatString="DD-MMM-YYYY" />
</declarations>
<s:Label id="myDateLabel"
text =" { dateFormatter.format( convertDateStringToUS( date ) ) } "/>
Related
I have a CSV file with following format:
<pre id="csv" style="display:none">
DATES,WHOLESALE,ECOMMERCE,RETAIL,LOANS,BONDISSUER
01/10/2018 00:00,25,16,13,1,0
01/10/2018 01:00,24,5,9,3,2
01/10/2018 02:00,28,6,17,0,6
The data range is 01/10/2018 00:00 - 31/10/2018 00:00
Interval is every hour.
I am using highstock stacked column with 5 categories: WHOLESALE,ECOMMERCE,RETAIL,LOANS,BONDISSUER.
My problem is, that the highstock navigator displays the data incorrectly. I think I have to customise property in range selector or navigator, but I can't find any documentation online. I tried inputDateParser, but it didn't work. Here is the jsfiddle
inputDateParser: function (value) {
value = value.split(/[:\.]/);
return Date.UTC(
1970,
0,
1,
parseInt(value[0], 10),
parseInt(value[1], 10),
parseInt(value[2], 10),
parseInt(value[3], 10)
);
}
How do I get the data range to be correct: month of October 2018 according to the dates in CSV?
I should not see a whole year in the navigator, when I only have data for October.
Thanks much appreciated
You would need to format the dates correctly, it can be done using the beforeParse callback function, like this:
data: {
csv: document.getElementById('csv').innerHTML,
beforeParse: function(e) {
let csv = e.split('\n'); //split by newline
let processedTable = []
processedTable.push(csv[0].split(','))
for (let i = 1; i < csv.length; i++) {
let row = csv[i].split(',');
if (row.length != 6) //skip empty rows or rows with more/less columns
continue;
let date = row[0].split(' ')[0].split('/')
let time = row[0].split(' ')[1].split(':')
processedTable.push(
[(new Date(date[2], date[1] - 1, date[0], time[0], time[1], 0)).getTime(), //get the timestamp for the date
parseInt(row[1]),
parseInt(row[2]),
parseInt(row[3]),
parseInt(row[4]),
parseInt(row[5])
].join(',')
)
}
return processedTable.join('\n') //join the array into a string again
},
},
Every row is parsed, by splitting it apart, the date is found, and milliseconds since 1970 is returned by getTime(). Then we join the cells into strings, and lastly the rows into a long string. The reason we convert this back into a string, is because highcharts is going to read it in from a string.
Working JSFiddle example: https://jsfiddle.net/ewolden/spmtgv3a/
API on beforeParse: https://api.highcharts.com/highcharts/data.beforeParse
I'm trying to create a spline chart using this CSV:
slave_id,date,time,rtc_temp,temp1,temp2,temp3
1,2017/12/26,16:42:59,21,11.50,13.13,5.88
2,2017/12/26,16:43:29,21,14.13,20.63,99.99
1,2017/12/26,16:44:00,21,11.50,13.13,5.88
2,2017/12/26,16:44:30,21,14.13,20.63,99.99
1,2017/12/26,16:45:01,21,11.50,13.13,5.88
2,2017/12/26,16:45:31,21,14.13,20.63,99.99
1,2017/12/26,16:46:02,21,11.50,13.13,5.88
2,2017/12/26,16:46:32,21,14.13,20.63,99.99
As you can see here [IMAGE], the graph is showing the date and time, but the x Axis is not accepting the date / time.
Ive tried using date.UTC, but that did not work either. Can someone point me in the right direction?
https://jsfiddle.net/asvoy6b9/ [not working due to CSV missing]
Full code [Hastebin]
I see that date variable in your code is a string:
// all data lines start with a double quote
line = line.split(',');
date = line[1] + " " + line[2];
(...)
RTC.push([
date,
parseInt(line[3], 10)
]);
If you choose to construct the point's options as an array of two values and the first value is a string then it's treated as its name property (not x).
Explanation: https://www.highcharts.com/docs/chart-concepts/series
In that case Highcharts assigns subsequent integers as x values for all points (that's why there're values like 00:00:00.000 (1 Jan 1970), 00:00:00.001 etc.).
You need to parse your date to timestamp. You can use Date.UTC() (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC) or some other function for this.
I've managed to get it working with Date.UTC using the following code:
var yyyymmdd = line[2].split("-"); //Split the date: 2017 12 16
var hhmmss = line[3].split(":"); //Split the time: 16 11 14
var date = Date.UTC(yyyymmdd[0], yyyymmdd[1] - 1, yyyymmdd[2], hhmmss[0], hhmmss[1], hhmmss[2]); //Stitch 'em together using Date.UTC
I have a long-term problem. I watch on the web, but I did not find right answer.
When I send data from WebAPI-Angular-Controller to Controller is a problem with formatting date. There are real data:
My TimeZone is UTC + 1
MS SQL:
Column type: DateTime2(3) value: 4.7.2015 20:00:00
The client receives the following formats based on the following criteria:
When I edit on WebApiConfig
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add (new IsoDateTimeConverter DateTimeStyles = {} DateTimeStyles.AdjustToUniversal); The client receives 2015-07-04T03: 00: 00Z and this {{time | date 'HH: mm: ss'}} show wrong time. It is show time + 2 hours -> 22:00:00 -> I tried {{time | date 'HH: mm: ss' 'UTC'}}, bud show this time - 1 hour -> 19:00:00.
When I edit on WebApiConfig
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;, so it is the same as first example.
When I edit on WebApiConfig
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local, so it is the same as first example excepting client receive data; they look like 2015-07-04T22: 00: 00 + 02: 00
When I edit on WebApiConfig
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Undefined, client receive data; they look like 2015-07-04T20: 00: 00 -> it is look OK, bud problem is elsewhere.
I need this time (20:00:00) - curent time (f.e. 10:00:00) = 10:00:00 diff time, but angular show 11:00:00, why?
there is source from angularController
var d1 = new Date(bq.Done_Time)
var d2 = new Date()
bq.Time_Left = new Date(bq.Done_Time).getTime() - new Date().getTime()
Is the problem on server side or client side? And how can I resolve it?
Thank you very much for your valuable suggestions
Dates are stored in the format, YYYY-mm-dd hh:mm:ss in the database.
e.g. 2014-07-03 00:00:00
But I want my Ext.Field.Date to have the format:
Y-m-d H:i
As per my component set up:
xtype: 'datefield',
id: 'p_p_start',
fieldLabel: 'Planned Start',
name: 'plannedstart',
allowBlank: false,
vtypeText: 'Date not valid',
format: 'Y-m-d H:i',
It is quite reasonable of me to want to be able to display the date in browsers other than just Chrome. To-date, FireFox and Safari will display nothing.
I've looked at a similar issue on SO which proposed support of all browser by splitting and reassembling the string format in a variation of:
var c=Ext.getCmp('p_p_start');
var st_date = "2014-05-08";// with alt formats changed Y-m-d, leaving time out diags
var dateParts = st_date.split(/-/);
var d = new Date(dateParts[0],parseInt( dateParts[1], 10) -1,dateParts[2]);
console.log("Date parts= " + dateParts);//Date parts= 2014,05,08
c.setValue( d ); //NOPE!
I was very happy with progress in Chrome (no problems at all) but have yet to succeed in getting anything working in FF or Safari (not tried IE but it has to work in that also). So, in summary, the question is,
How do I get date formats in all browsers to work with the format Y-m-d H:i using setValue?
Many thanks in advance.
Kevin
A date field's internal value is a JavaScript Date Object, which is not tied to a specific format. The format config simply defines the display and input format of the field.
You can use setValue with a string, but only if the string already conforms to your defined format.
In your case the problem seems to be converting a date string value with a different format which was received from the server/database to a Date object.
Have a look at the Ext.Date singleton, specifically its function Ext.Date.parse, to achieve that:
var st_date = "2014-05-08";
var dateObj = Ext.Date.parse(st_date, "Y-m-d");
Ext.getCmp('p_p_start').setValue(dateObj);
var st_date2 = "2014-07-03 00:00:00";
var dateObj2 = Ext.Date.parse(st_date2, "Y-m-d H:i:s");
Ext.getCmp('p_p_start').setValue(dateObj2);
I have a loop to select 'LibrarySwaps' for tomorrow.
This works, but not when I set days_ahead default to 1 (it returns records with todays date).
Why do I need to add 2 to the date to get a day than is only 1 day in the future?
I am doing this 11am EST so this is not a time zone issue with that and UTC both being the same day... I thought maybe 'cos one side has a time component and the other doesn't but nope, I'm using date() for the sql and Date + 1.days for the ruby. I may switch to (one date minus the other date) and look at the result.
Thanks!
Returns Tomorrows (uses 2):
def self.find_future_swaps(days_ahead=2)
#upcoming_swaps = LibrarySwap.all(:conditions => ['date(suggested_date) = ?',Date.today + days_ahead.day ])
end
Returns Todays (uses 1):
def self.find_future_swaps(days_ahead=1)
#upcoming_swaps = LibrarySwap.all(:conditions => ['date(suggested_date) = ?',Date.today + days_ahead.day ])
end
MySQL is likely storing your suggested_date field in UTC. So an entry from 10pm on 4/29 would actually be stored as 3am on 4/30 (assuming you're in the Eastern timezone).
You can do this to add the offset to the times you're searching for:
#upcoming_swaps = LibrarySwap.all(:conditions => ['date(convert_tz(suggested_date,'+00:00','-05:00')) = ?',Date.today + days_ahead.day ])