Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 11 of 11

Thread: Date conversions

  1. #1
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,271

    Default Date conversions

    One thing I absolutely hate in JavaScript is the date formats and how an sc date from a record is konverted to

    "Thu May 08 2008 14:50:18 GMT+0200 (Romance Daylight Time)"

    I need to use the date in a query but when I construct the query like this

    Code:
    var sqlto = "number = \""+toplevelnumber+"\" and description,1 = \""+FromActivityFile.description[0]+"\" and operator = \""+FromActivityFile.operator+"\" and datestamp = \""+FromActivityFile.datestamp+"\" "
    The query will always fail because the date is inserted into the sql string as above instead of the standard 08/05/2008 00:00:00 format.

    I can't figure out which of the multitude of date functions I need to use to get the date in the proper format. Does anybody know ?
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    291

    Default

    Hi Tommy,

    did you try ... and datestamp = '"+system.functions.str(FromActivityFile.datestamp )+"' "
    why do you enclose the date with " and not with ' ???
    Greetings,
    Lars

  3. #3
    Senior Member benvargas's Avatar
    Join Date
    Apr 2005
    Location
    San Diego, CA USA
    Posts
    194

    Default

    Tommy, you might find the following tidbit helpful... comes from a notepad document I through useful things I see into (i.e. I don't remember the source):

    // JavaScript that creates a SC date/time which is converted a JavaScript date automatically. Then converting JS date to XMLDate, then to SC date/time.

    var scDate = system.functions.tod();
    print("scDate was created from an SC date/time, auto converted to JS Date shown by its value of: " + scDate);

    print("Now converting the JS Date to a new XMLDate object...");
    var xmlDate = new XMLDate( scDate );

    print( "The value of the new XMLDate object is: " + xmlDate );

    print("Now converting the XMLDate object to a ServiceCenter-formatted date/time string...");
    var newSCDate = xmlDate.getSCDateTimeString();

    print("The ServiceCenter-formatted date/time string: " + newSCDate);
    Last edited by benvargas; 2008-05-08 at 21:35.

  4. #4
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Talking

    Quote Originally Posted by benvargas View Post
    Tommy, you might find the following tidbit helpful... comes from a notepad document I through useful things I see into (i.e. I don't remember the source):

    // JavaScript that creates a SC date/time which is converted a JavaScript date automatically. Then converting JS date to XMLDate, then to SC date/time.

    var scDate = system.functions.tod();
    print("scDate was created from an SC date/time, auto converted to JS Date shown by its value of: " + scDate);

    print("Now converting the JS Date to a new XMLDate object...");
    var xmlDate = new XMLDate( scDate );

    print( "The value of the new XMLDate object is: " + xmlDate );

    print("Now converting the XMLDate object to a ServiceCenter-formatted date/time string...");
    var newSCDate = xmlDate.getSCDateTimeString();

    print("The ServiceCenter-formatted date/time string: " + newSCDate);
    That is a good function to remind the date format :-)

  5. #5
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Question date ranges

    Is there a function which can merge an array of dates
    e.g.:
    d[1] = 01.05.08 - 12.05.08
    d[2 ]= 10.05.08 - 18.05.08
    d[3] = 20.05.08 - 23.05.08
    etc ...
    result schould be
    r = 01.05.08 - 18.05.08
    20.05.08 - 23.05.08
    ...

    Thx
    Tuncay

  6. #6
    Senior Member mateuszk's Avatar
    Join Date
    Nov 2006
    Posts
    302

    Default

    This looks like a nice challenge, some additional questions:

    1. what is the format of the array?
    d[1] = [01.05.08,12.05.08]
    d[1] = [01.05.08,d0t1h]

    2. what kind of objects are stored in the array, XMLDate, JS Date?

    Let me know if you're still interested in a solution.

  7. #7
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Default

    I am very interested in a solution.
    The format of the array could be 2 dimensional: Begin, End.
    The format doen´t matter, because after the calculation there is the posibility to transform with the nice datefunctions :-)

    thanx for any hints
    Tuncay

  8. #8
    Senior Member mateuszk's Avatar
    Join Date
    Nov 2006
    Posts
    302

    Default

    OK, here you go:

    Code:
    var dates = [],datesRange=[],i=1,ir=0,d1,d2;
    
    //fill dates
    //first position in the array is the most future date, sort check.
    
    dates[0] = [new Date(2008,05,30),new Date(2008,05,33)]
    do {
     d1 = new Date(2008,05,25+i); d2 = new Date(2008,05,25+i+2);
     dates[i] = [d1,d2]; i++; 
    } while(i<5)
    
    //adding another, separate, range. making sure they will "split"
    dates[5] = [new Date(2008,04,30),new Date(2008,05,12)]
    do {
     d1 = new Date(2008,04,11+i); d2 = new Date(2008,05,10+i);
     dates[i] = [d1,d2]; i++; 
    } while(i<10)
    
    
    //sort the dates array
    dates.sort(datesArraySort);
    
    for(a in dates) print(dates[a][0].toUTCString(),' to ',dates[a][1].toUTCString());
    print('sorted:');
    
    //fill the first element of the final ranges array
    datesRange[ir++] = dates[0];
    
    for(i=1;i<dates.length;i++) {
    
      d1 = dates[i][0].getTime(); //curr range start
      d2 = dates[i][1].getTime(); //curr range end
      dprev2 = datesRange[ir-1][1].getTime(); //prev range end
      
      //switch ending date with current
      if(dprev2 < d2 && d1 < dprev2) datesRange[ir-1][1]=dates[i][1];
      //open new range, start & end from the current range
      else datesRange[ir++] = dates[i];
    
    }
    
    for(a in datesRange) print(datesRange[a][0].toUTCString(),' to ',datesRange[a][1].toUTCString());
    print('final:')
    
    /* 
    sort the dates array, 
    first on the range start, 
    if ranges starts are equal, sort on ends
    */
    function datesArraySort(d1,d2) {
     
     dd1=d1[0].getTime(),dd2=d2[0].getTime();
     ret=dd1-dd2;
     if(ret!=0) return ret;
     else {
      dd1=d1[1].getTime(),dd2=d2[1].getTime();
      return dd1-dd2;
     }
    }
    Let me know if this is what you wanted.

  9. #9
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Talking

    Quote Originally Posted by mateuszk View Post
    OK, here you go:

    Let me know if this is what you wanted.
    Thank you mateuszk,
    I think it is that what I need. I will test it on my datasets.
    When you allow I would like to embed this code in my absence script ...
    Tuncay

  10. #10
    Senior Member mateuszk's Avatar
    Join Date
    Nov 2006
    Posts
    302

    Default

    Quote Originally Posted by tuncay View Post
    When you allow I would like to embed this code in my absence script ...
    Sure, let's just assume this is license-free code.

    In case you'd have more requests like that
    drop me an email and I'm sure I'll find some spare time for a quick brain-teaser
    (actually, the weirder the assignment the better)


    Cheers.

  11. #11
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Smile

    Thanx,
    I will keep it in mind

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts