Get Date details (day, month, year) in GWT - datetime

I need to get the day, month, year details from a Date value but getYear() is deprecated, gives year on 2 digits, and has problems with Y2K (2008 gives 108). The java doc recommends using java.util.calendar but it is not supported in GWT.
I want to avoid sending all the info back and forth between the server and client just to deal with dates.
Edit: Calendar might be supported Date handling functions should be implemented in GWT futur versions : http://code.google.com/p/google-web-toolkit/issues/detail?id=603

The workaround presented by Ashwin works and it's not that tricky. But I'd suggest using the date patterns instead of splitting:
For date -
DateTimeFormat.getFormat( "d" ).format( new Date() )
For month -
DateTimeFormat.getFormat( "M" ).format( new Date() )
For year -
DateTimeFormat.getFormat( "yyyy" ).format( new Date() )

Do not use those deprecated methods on Date class in GWT.
If you don't want to use third party Date implementations for GWT, You use a combination of DateTimeFormat along with string manipulation as a workaround for the time being, until GWT comes up with some better support for manipulating dates.
For date -
DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[0]
For month -
DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[1]
For year -
DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[2]
Edit-
Similarly, avoid using new Date( yy, mm, dd ) has come inconsistencies depending on the browser and date range.
I have use a simple DateUtil Class to create and parse Date objects in GWT, maybe of some use to you -
(Warning: Very crude, and work in progress)
public class DateUtil
{
private static final String D_M_YYYY = "d-M-yyyy";
private static final String DATE_SEPARATOR = "-";
public static Date getDate( Integer dd, Integer mm, Integer yyyy )
{
if ( dd == null || mm == null || yyyy == null )
return null;
Date retVal = null;
try
{
retVal = DateTimeFormat.getFormat( D_M_YYYY ).parse( dd + DATE_SEPARATOR + mm + DATE_SEPARATOR + yyyy );
}
catch ( Exception e )
{
retVal = null;
}
return retVal;
}
public static String getDayAsString( Date date )
{
return ( date == null ) ? null : DateTimeFormat.getFormat( D_M_YYYY ).format( date ).split( DATE_SEPARATOR )[0];
}
public static String getMonthAsString( Date date )
{
return ( date == null ) ? null : DateTimeFormat.getFormat( D_M_YYYY ).format( date ).split( DATE_SEPARATOR )[1];
}
public static String getYearAsString( Date date )
{
return ( date == null ) ? null : DateTimeFormat.getFormat( D_M_YYYY ).format( date ).split( DATE_SEPARATOR )[2];
}
public static boolean isValidDate( Integer dd, Integer mm, Integer yyyy )
{
boolean isvalidDate = true;
try
{
String transformedInput = DateTimeFormat.getFormat( D_M_YYYY ).format( getDate( dd, mm, yyyy ) );
String originalInput = dd + DATE_SEPARATOR + mm + DATE_SEPARATOR + yyyy;
isvalidDate = transformedInput.equals( originalInput );
}
catch ( Exception e )
{
isvalidDate = false;
}
return isvalidDate;
}
}

You may just add 1900 to getYear() return value

I don't think gwt will support Calendar in the future, may be it could support another date manipulation implementation.
So, because the java recommendation about not using Date is not valid in Gwt and you have not any other option without importing a third party library, the right way is to use Date and ignore deprecation warnings.

Related

datetime parsing from excel sheet in asp.net

i want to get a datetime value from excel sheet and take the highest and the lowest date
i read the excel sheet and put it in datatable :
i tried this code :
protected void CheckTheFP(DataTable data)
{
if (data.Rows.Count != 0)
{
DateTime ds = new DateTime();
err.Text = DateTime.TryParseExact(data.Rows[0][2].ToString(), "MM/dd/yy hh:mm tt",
CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out ds) + "" ;
}
}
but i always get false ... don't know why ?
and is there a way to sort this datatable or take the highest and lowest date
this the excel sheet i read from
This format string should work: "M/dd/yy h:mm tt". I've used single M because the month has one digit, the same applies to the hours. I've used CultureInfo.InvariantCulture to prevent that all / will be replaced with your actual date-separator (in case that it's different).
You can use LINQ:
var allDateTimes = data.AsEnumerable()
.Select(row => DateTime.ParseExact(row.Field<string>("Time"), "M/dd/yy h:mm tt", CultureInfo.InvariantCulture));
DateTime min = allDateTimes.Min();
DateTime max = allDateTimes.Max();
If you want to be on the safe side you should use TryParseExact, for example with this code:
IEnumerable<DateTime> allDateTimes = data.AsEnumerable()
.Select(row => {
string time = row.Field<string>("Time").Trim();
DateTime dt;
if (DateTime.TryParseExact(time, "M/dd/yy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
return (DateTime?) dt;
return null; // set a breakpoint here to see which value could not be parsed
})
.Where(dt => dt.HasValue)
.Select(dt => dt.Value);
DateTime min = allDateTimes.Min();
DateTime max = allDateTimes.Max();
Edit: you: "when i try to use it on the date 11/2/14 4:42 PM you see the 11 is not in M datetime format
The month is not the problem. Use single d instead because the days can have a single digit also.
So: "M/d/yy h:mm tt"

getting an int to decrement after an action

so in my person table...I have Id, Name & HolidaysRemaining.
Its for a holiday booking application, and atm when a user selects dates from a calendar and clicks the button, each date selected will be stored in the DB, I am trying to minus the holidays remaining by 1, as each holiday is booked, but it doesn't seem to be picking up.
//listHolidays in correct format dd/mm/yy
[HttpPost]
public ActionResult listHolidays(Holiday holiday, Person person , int? PersonId, string HolidayDate, string endDate, string AlreadyExists)
{
db.People.Attach(person);
//int holidaysRemaining = 20;
//person.HolidaysRemaining = holidaysRemaining;
DateTime startDates = Convert.ToDateTime(HolidayDate);
DateTime endDates = Convert.ToDateTime(endDate);
try{
while (startDates <= endDates)
{
if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
{
//if user selects Holiday that already exists, wont add it to Db
//gets string, and uses the previously converted to dateTime 'startDate'
//id so only applies to person creating holidays
ViewBag.CantDuplicateHolidays = String.IsNullOrEmpty(AlreadyExists) ? "date" : "";
var dates = from d in db.Holidays
where d.HolidayDate == startDates && d.PersonId == PersonId
select d;
// <= 0..so if holiday does not already exist
if (dates.Count() <= 0)
{
// holidaysRemaining--;
person.HolidaysRemaining = person.HolidaysRemaining - 1;
Holiday holiday1 = new Holiday();
holiday1.PersonId = PersonId.Value;
holiday1.HolidayDate = startDates;
db.Holidays.AddObject(holiday1);
db.SaveChanges();
//say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
//but doesnt chage the value of startdates = 'startdates'
}
}
}
startDates = startDates.AddDays(1);
}
finally
{
db.People.Detach();
}
return RedirectToAction("Index");
}
Probably this is the easiest solution.
replace:
person.HolidaysRemaining = person.HolidaysRemaining - 1;
with:
var dbPerson = from p in db.People where p.Id == PersonId select p;
dbPerson[0].HolidaysRemaining--;
Alternatively we were discussing attaching the person object since you have it:
db.People.Attach(person)
try {
// ... loop and everything else here
} finally {
db.People.Detach(person);
}
} // end of method
But this is a bit more brittle, and would only be necessary if there's not already a Person object in db.People.
Note: It seems a little weird that both person and PersonId are passed into listHolidays().
I think your problem is here:
if (dates.Count() <= 0)
{
// holidaysRemaining--;
person.HolidaysRemaining--;
Try changing it to:
if (dates.Count() <= 0)
{
// holidaysRemaining--;
person.HolidaysRemaining = person.HolidaysRemaining - 1;
EDIT
Also, you never actually update the database with person?
db.People.Attach(person);
before db.SaveChanges();
EDIT AGAIN
Try this:
[HttpPost]
public ActionResult listHolidays(Holiday holiday, Person person, int? PersonId, string HolidayDate, string endDate, string AlreadyExists)
{
//int holidaysRemaining = 20;
//person.HolidaysRemaining = holidaysRemaining;
DateTime startDates = Convert.ToDateTime(HolidayDate);
DateTime endDates = Convert.ToDateTime(endDate);
while (startDates <= endDates)
{
if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
{
//if user selects Holiday that already exists, wont add it to Db
//gets string, and uses the previously converted to dateTime 'startDate'
//id so only applies to person creating holidays
ViewBag.CantDuplicateHolidays = String.IsNullOrEmpty(AlreadyExists) ? "date" : "";
var dates = from d in db.Holidays
where d.HolidayDate == startDates && d.PersonId == PersonId
select d;
// <= 0..so if holiday does not already exist
if (dates.Count() <= 0)
{
// holidaysRemaining--;
person.HolidaysRemaining = person.HolidaysRemaining - 1;
Holiday holiday1 = new Holiday();
holiday1.PersonId = PersonId.Value;
holiday1.HolidayDate = startDates;
db.Holidays.AddObject(holiday1);
db.People.Attach(person);
db.SaveChanges();
//say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
//but doesnt chage the value of startdates = 'startdates'
}
}
startDates = startDates.AddDays(1);
}
return RedirectToAction("Index");
}

Difference in years,months and days between 2 hijri dates

Please can anyone provide a method to calculate the difference between 2 hijri dates thanks in advance
i tried this code
HijriCalendar hijriCal=new HijriCalendar();
DateTimeFormatInfo DTFormat = new System.Globalization.CultureInfo("ar-sa", false).DateTimeFormat;
DTFormat.Calendar = new System.Globalization.HijriCalendar();
DTFormat.ShortDatePattern = "dd/MM/yyyy";
string HijriDate = FromDate.Date.ToString("d", DTFormat);
string[] fromDateParams=HijriDate.Split('/');
HijriDate = ToDate.Date.ToString("d", DTFormat);
string[] toDateParams = HijriDate.Split('/');
DateTime fromDateHijri = new DateTime(hijriCal.GetYear(FromDate), hijriCal.GetMonth(FromDate), int.Parse(fromDateParams[0]), hijriCal);
DateTime toDateHijri = new DateTime(hijriCal.GetYear(ToDate), hijriCal.GetMonth(ToDate), int.Parse(toDateParams[0]), hijriCal);
TimeSpan ts = ToDate.Subtract(FromDate);
As long as you just store the dates in normal datetimes, just treat them as any other date.
public TimeSpan GetDifference(this DateTime date1, DateTime date2) {
if (date1 < date2) {
return date2 - date1;
}
else if (date1 > date2) {
return date1 - date2;
}
return new TimeSpan(0);
}

Best Way To Get All Dates Between DateA and DateB

I am using an asp:Calander and I have an object that has a beginning date and an ending date. I need to get all the dates between these two dates and place them in an array so i can then render corresponding dates on the calander with different CSS
DateTime startDate;
DateTime endDate;
DateTime currentDate = startDate;
List<DateTime> dates = new List<DateTime> ();
while (true)
{
dates.Add (currentDate);
if (currentDate.Equals (endDate)) break;
currentDate = currentDate.AddDays (1);
}
It assumes that startDate < than endDate, you get the results on the "dates" list
IEnumerable<DateTime> RangeDays(DateTime RangeStart, DateTime RangeEnd) {
DateTime EndDate = RangeEnd.Date;
for (DateTime WorkDate = RangeStart.Date; WorkDate <= EndDate; WorkDate = WorkDate.AddDays(1)) {
yield return WorkDate;
}
yield break;
}
Untested code... but should work.
I voted up AlbertEin because he gave a good answer, but do you really need a collection to hold all the dates? When you are rendering the day, couldn't you just check if the date is withing the specified range, and then render it differently, no need for a collection. Here's some code to demonstrate
DateTime RangeStartDate,RangeEndDate; //Init as necessary
DateTime CalendarStartDate,CalendarEndDate; //Init as necessary
DateTime CurrentDate = CalendarStartDate;
String CSSClass;
while (CurrentDate != CalendarEndDate)
{
if(CurrentDate >= RangeStartDate && CurrentDate <= RangeEndDate)
{
CSSClass= "InRange";
}
else
{
CSSClass = "OutOfRange";
}
//Code For rendering calendar goes here
currentDate = currentDate.AddDays (1);
}
// inclusive
var allDates = Enumerable.Range(0, (endDate - startDate).Days + 1).Select(i => startDate.AddDays(i));
// exclusive
var allDates = Enumerable.Range(1, (endDate - startDate).Days).Select(i => startDate.AddDays(i));

Actionscript 3 - Fastest way to parse yyyy-mm-dd hh:mm:ss to a Date object?

I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss] into a Date object. Here are the 3 ways I have tried doing it and the times it takes each method to parse 50,000 date time strings.
Does anyone know any faster ways of doing this or tips to speed up the methods?
castMethod1 takes 3673 ms
castMethod2 takes 3812 ms
castMethod3 takes 3931 ms
Code:
private function castMethod1(dateString:String):Date {
if ( dateString == null ) {
return null;
}
var year:int = int(dateString.substr(0,4));
var month:int = int(dateString.substr(5,2))-1;
var day:int = int(dateString.substr(8,2));
if ( year == 0 && month == 0 && day == 0 ) {
return null;
}
if ( dateString.length == 10 ) {
return new Date(year, month, day);
}
var hour:int = int(dateString.substr(11,2));
var minute:int = int(dateString.substr(14,2));
var second:int = int(dateString.substr(17,2));
return new Date(year, month, day, hour, minute, second);
}
-
private function castMethod2(dateString:String):Date {
if ( dateString == null ) {
return null;
}
if ( dateString.indexOf("0000-00-00") != -1 ) {
return null;
}
dateString = dateString.split("-").join("/");
return new Date(Date.parse( dateString ));
}
-
private function castMethod3(dateString:String):Date {
if ( dateString == null ) {
return null;
}
var mainParts:Array = dateString.split(" ");
var dateParts:Array = mainParts[0].split("-");
if ( Number(dateParts[0])+Number(dateParts[1])+Number(dateParts[2]) == 0 ) {
return null;
}
return new Date( Date.parse( dateParts.join("/")+(mainParts[1]?" "+mainParts[1]:" ") ) );
}
No, Date.parse will not handle dashes by default. And I need to return null for date time strings like "0000-00-00".
I've been using the following snipplet to parse UTC date strings:
private function parseUTCDate( str : String ) : Date {
var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)Z/);
var d : Date = new Date();
d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));
d.setUTCHours(int(matches[4]), int(matches[5]), int(matches[6]), 0);
return d;
}
Just remove the time part and it should work fine for your needs:
private function parseDate( str : String ) : Date {
var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d)/);
var d : Date = new Date();
d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));
return d;
}
No idea about the speed, I haven't been worried about that in my applications. 50K iterations in significantly less than a second on my machine.
This was the fastest I could come up with after some fiddling:
private function castMethod4(dateString:String):Date {
if ( dateString == null )
return null;
if ( dateString.length != 10 && dateString.length != 19)
return null;
dateString = dateString.replace("-", "/");
dateString = dateString.replace("-", "/");
return new Date(Date.parse( dateString ));
}
I get 50k iterations in about 470ms for castMethod2() on my computer and 300 ms for my version (that's the same amount of work done in 63% of the time). I'd definitely say both are "Good enough" unless you're parsing silly amounts of dates.
I'm guessing Date.Parse() doesn't work?
Well then method 2 seems the best way:
private function castMethod2(dateString:String):Date {
if ( dateString == null ) {
return null;
}
if ( dateString.indexOf("0000-00-00") != -1 ) {
return null;
}
dateString = dateString.split("-").join("/");
return new Date(Date.parse( dateString ));
}
Because Date.parse() does not accept all possible formats, we can preformat the passed dateString value using DateFormatter with formatString that Data.parse() can understand, e.g
// English formatter
var stringValue = "2010.10.06"
var dateCommonFormatter : DateFormatter = new DateFormatter();
dateCommonFormatter.formatString = "YYYY/MM/DD";
var formattedStringValue : String = dateCommonFormatter.format(stringValue);
var dateFromString : Date = new Date(Date.parse(formattedStringValue));
var strDate:String = "2013-01-24 01:02:40";
function dateParser(s:String):Date{
var regexp:RegExp = /(\d{4})\-(\d{1,2})\-(\d{1,2}) (\d{2})\:(\d{2})\:(\d{2})/;
var _result:Object = regexp.exec(s);
return new Date(
parseInt(_result[1]),
parseInt(_result[2])-1,
parseInt(_result[3]),
parseInt(_result[4]),
parseInt(_result[5]),
parseInt(_result[6])
);
}
var myDate:Date = dateParser(strDate);
Here is my implementation. Give this a try.
public static function dateToUtcTime(date:Date):String {
var tmp:Array = new Array();
var char:String;
var output:String = '';
// create format YYMMDDhhmmssZ
// ensure 2 digits are used for each format entry, so 0x00 suffuxed at each byte
tmp.push(date.secondsUTC);
tmp.push(date.minutesUTC);
tmp.push(date.hoursUTC);
tmp.push(date.getUTCDate());
tmp.push(date.getUTCMonth() + 1); // months 0-11
tmp.push(date.getUTCFullYear() % 100);
for(var i:int=0; i < 6/* 7 items pushed*/; ++i) {
char = String(tmp.pop());
trace("char: " + char);
if(char.length < 2)
output += "0";
output += char;
}
output += 'Z';
return output;
}

Resources