Answered by:
Convert Date and return long string

Question
-
User-1952516322 posted
Hello,
I have two cases:
firstly: I have a date, and I read it correctly, but when I want to get just the month or the day, it is not correct.
Example:
var x = new Date(DateFromDataBase); >> The date from Database > 1988-11-19
var Year = x.getFullYear() >> This read the year correct. > 1988
var Month = x.getMonth () >> Read wrong month. > 10 ??
var Day = x.getDay() >> also this read wrong day. > 06 ??
Second Case: I want to return design depends on value from Database
Example: function CheckHasFile(val) { var DesignTD; $.ajax({ url: '/Project.API/Test?id=x'.replace('x', val), type: 'post', dataType: 'json', contentType: 'application/json;charset=utf-8', success: function (data) { var HasVisits = Boolean(data); if (HasVisits) { DesignTD = "<img src=/Images/DownloadFile.png width=25 height=25 /><img src=/Images/FileSize.png width=25 height=25 />"; } else { DesignTD = "<img src=/Images/Download.png width=25 height=25 />"; } }, error: function (error) { alert(error.statusText); } }); return DesignTD; }
the DesignTD always return "undefined" .. not change the value inside ajax..... I made a debugging and the code run correctly also when I checked the new value for 'DesignTD' inside the ajax, it read, but when the compiler finish the ajax, and I checked the value on return > it is "undefined" ?
Thanks
Friday, March 29, 2019 2:07 PM
Answers
-
User-474980206 posted
you should read the javascript docs:
getMonth() return the month, zero based (0-11). so jan == 0
getDay() return the day of the week (0-6) sunday == 0
getDate() return the calendar day (1-31)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 29, 2019 2:27 PM -
User438705957 posted
I use the Moment date library which has a lot of handy date functions. Available at http://momentjs.com/
To parse an incoming date:
m = moment('2013-03-01', 'YYYY-MM-DD')
To format the date once it is a moment date
.format('L') // 06/09/2014 .format('l') // 6/9/2014 .format('LL') // June 9 2014 .format('ll') // Jun 9 2014 .format('LLL') // June 9 2014 9:32 PM .format('lll') // Jun 9 2014 9:32 PM .format('LLLL') // Monday, June 9 2014 9:32 PM .format('llll') // Mon, Jun 9 2014 9:32 PM
A Moment cheat-sheet available at https://devhints.io/moment
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 1, 2019 12:15 AM -
User839733648 posted
Hi Khalid Salameh,
According to your description about second issue, I suggest that you could modify the attribute async in ajax.
Since the default async is true and you could not return the value out of ajax.
If you need synchronous requests, you could set this option to false.
function CheckHasFile(val) { var DesignTD; $.ajax({ url: '/Project.API/Test?id=x'.replace('x', val), type: 'post', dataType: 'json', contentType: 'application/json;charset=utf-8',
async:'false', success: function (data) { var HasVisits = Boolean(data); if (HasVisits) { DesignTD = "<img src=/Images/DownloadFile.png width=25 height=25 /><img src=/Images/FileSize.png width=25 height=25 />"; } else { DesignTD = "<img src=/Images/Download.png width=25 height=25 />"; } }, error: function (error) { alert(error.statusText); } }); return DesignTD; }Reference:http://api.jquery.com/jquery.ajax/
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 1, 2019 6:00 AM
All replies
-
User-474980206 posted
you should read the javascript docs:
getMonth() return the month, zero based (0-11). so jan == 0
getDay() return the day of the week (0-6) sunday == 0
getDate() return the calendar day (1-31)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 29, 2019 2:27 PM -
User-1952516322 posted
Thanks bruce,
It is working for the date, If you know how to resolve the second case, help me for that, Thank you.
Sunday, March 31, 2019 6:04 AM -
User438705957 posted
I use the Moment date library which has a lot of handy date functions. Available at http://momentjs.com/
To parse an incoming date:
m = moment('2013-03-01', 'YYYY-MM-DD')
To format the date once it is a moment date
.format('L') // 06/09/2014 .format('l') // 6/9/2014 .format('LL') // June 9 2014 .format('ll') // Jun 9 2014 .format('LLL') // June 9 2014 9:32 PM .format('lll') // Jun 9 2014 9:32 PM .format('LLLL') // Monday, June 9 2014 9:32 PM .format('llll') // Mon, Jun 9 2014 9:32 PM
A Moment cheat-sheet available at https://devhints.io/moment
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 1, 2019 12:15 AM -
User839733648 posted
Hi Khalid Salameh,
According to your description about second issue, I suggest that you could modify the attribute async in ajax.
Since the default async is true and you could not return the value out of ajax.
If you need synchronous requests, you could set this option to false.
function CheckHasFile(val) { var DesignTD; $.ajax({ url: '/Project.API/Test?id=x'.replace('x', val), type: 'post', dataType: 'json', contentType: 'application/json;charset=utf-8',
async:'false', success: function (data) { var HasVisits = Boolean(data); if (HasVisits) { DesignTD = "<img src=/Images/DownloadFile.png width=25 height=25 /><img src=/Images/FileSize.png width=25 height=25 />"; } else { DesignTD = "<img src=/Images/Download.png width=25 height=25 />"; } }, error: function (error) { alert(error.statusText); } }); return DesignTD; }Reference:http://api.jquery.com/jquery.ajax/
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 1, 2019 6:00 AM -
Monday, April 1, 2019 1:33 PM
-
User475983607 posted
Setting async to false is not a good idea because it creates a blocking request and this feature will be turned off in future browser releases.
Rather you should rethink the design and place the DOM update in the success handlers. Or better, take advantage of the .done() promise and place your UI update code in the the .done();
Monday, April 1, 2019 1:42 PM -
User438705957 posted
Agree. Just been thru a month long process to do exactly that. Virtually had to re-architect the whole website.
It's painful to get working but makes sense in the long run, especially given that synchronous processing will be deprecated in future browser releases.
Me thinks a lot of websites will cease to work when this happens!Tuesday, April 2, 2019 9:08 PM