I cant seem to figure out how to save a date coming from a date input field. Whenever I try submit, the date's format is pretty weird. It always goes something like this:
Wed Jun 17 2015 08:00:00 GMT+0800 (Taipei Standard Time)
and saving it on my DB gets messy. I need to use Carbon to make it work.
Is there a way to do save date from a Date Input Field flawlessly? If so, how?
Related
db.collection.insertMany({_id:123, name:"abc", loc:"Delhi", YOB:new Date("1991-08-17")}, {_id:124, name:"def", loc:"Bengaluru", YOB:Date("1996-03-08")})
I want to change date format from new Date to Date in first document. How to change a specific field from a specific document like this?
I tried to change the date value from New Date format i.e., ISODate("1991-08-17T00:00:00:000Z") to Date format i.e., Mon Aug 17 2022 03:19:39 GMT+0530 (India Standard Time)
Can anyone help me out?
Using angularjs, dynamodb as DB here.
I have a form where user saves some data. I save my "CreateOn" date in my dynamo db as:
DateTime.UtcNow.ToString("o");
//This saves date in DB as:2018-08-21T12:58:08.7823906Z
Storing like this because dynamo db requires dates (string) to stored in ISO8601 format if you want to use between operator to search for date range.
Now I have a search filters on my page which is basically an angular calendar. When the user selects the date in the calendar( start and end date) I want to get the data back based on the selected date. Here I am using moment to pass the calendar selected date to my api call as:
moment(createdOn).toISOString()
Eg: If they select the Today's date in the calendar I pass the selected date
(Tue Aug 21 2018 00:00:00 GMT-0400 (Eastern Daylight Time)) to the above function
The result of passing this date to moment(createdOn).toISOString() is
2018-08-21T04:00:00.000Z
The search condition at dynamo db is:
conditions.Add(new ScanCondition("CreatedOn", ScanOperator.Between, startDate, endDate ));
If the user selects from the calendar the start date as "08-20-2018" (2018-08-20T04:00:00.000Z) and the end date is "08-21-2018"(2018-08-21T04:00:00.000Z), the code all the data created b/w these 2 dates.
Now the issue is if they select same start and end date then the code does not returns any data, I believe because the start and end date is "2018-08-21T04:00:00.000Z" and the time part of this is all 0000 etc.
My question is how can I convert the date from my calendar ie my end date to correctly reflect the the end time which they select. I havent used ISO8601 format before so not sure how can I do so.
Thanks
You don't need moment for this. You can zero out the time using a Date in the following way.
const date = new Date('2018-08-21T12:58:08.7823906Z')
date.setUTCHours(0)
date.setUTCMinutes(0)
date.setUTCSeconds(0)
date.setUTCMilliseconds(0)
Then you can simply use toISOString() to format the date.
date.toISOString()
// returns '2018-08-21T00:00:00.000Z'
If you don't want to zero out the time, and instead want to set some specific time, you can use a similar approach, just substitute the 0 with whatever time you want.
Some other things to note: DynamoDB doesn't require any specific formatting for dates. DynamoDB simply does a string or number comparison depending on what the field is defined as. You could store your dates in DynamoDB as integers or another string format if you feel that would be easier to work with.
Also, I'm not sure how your table is setup but make sure that your "CreateOn" field is the Range key and that you are using Query, not Scan. Using the Scan operation doesn't scale well.
Here's the problem with my datepickers.I tried three approaches which ultimately gave same wrong result.
Input type date:
<input type="date" ng-model="d.e_date" class="date"/>
Result: returned 2016-03-31 on choosing 1 April 2016 and 2016-04-06 on choosing 7April 2016.(One Day Back)
UI Bootstrap's datepicker and Jquery's datepicker:
Result: they also gave same result for format "yyyy/MM/dd".while i used them with format "MM/yyyy" and they returned 12/2013 on choosing January 2014 and 11/2015 on choosing December 2015.
Conclusion: As far as i could understand,it is somehow subtracting one point.like one day back in full date format and one month back in month's format.
datepicker date off by one day
This is the link i checked and also tried replacing "-" with "/" as mentioned in above post but no luck.
Update:
I am actually getting this date value in Angular controller and sending it to server.In Controller date is returned in "Thu Apr 01 2010 00:00:00 GMT+0500" on choosing 1st april 2010 while when its is submitted to service as
console.log($scope.s_month);// returns this Thu Apr 01 2010 00:00:00 GMT+0500
Restangular.all("s_session/create").post({s_month:$scope.s_month}).then(function(response){
console.log(response);
});//returns '2010-03-31T19:00:00.000Z' according to browser's network tab.
I have an excel file in which the dob column contains entry of this type Thu Aug 04 00:00:00 BST 1983. I am trying to import the excel file in SQL-Server therefore I want to convert it to YYYY-MM-DD type. I tried the format cells option but it is not working. Any simple way of doing it or do I have to find regex for it?
Here is the sample file.
In case your string in excel is Thu Aug 04 00:00:00 BST 1983 with the same format, then you can try this:
Then goto Custom format cells and select Date first and then select Custom and type YYYY-MM-DD to get the desired format.
You can use Excel's DATE() function to do this. This takes year, month and day as values. There is some manipulation required to your date to get these values.
Please see below. 3rd row shows values: 30532 becomes 1983-08-04 when it is formatted (show formulae is on so you can see 2nd row)
new Date(moment().year(), moment().month(), moment().day(), vm.newHearing().HearingTime().split(":")[0], vm.newHearing().HearingTime().split(":")[1]).toLocaleString()
The client side value for a date column is 11/5/2013 10:15:00 AM. The time is selected from HTML5 time input control.
When I check in database after saving the entity, it shows me incorrect time value:
11/5/2013 3:15:00 PM
It appears that you are using moment.js, which is fine except you aren't using it properly
Try this instead:
moment(vm.newHearing().HearingTime(), "HH:mm").toISOString()
That will pass the selected time, on the current day, from the user's local time zone, converted to UTC time and in ISO format.
Now that might not be exactly what you want to do. Depending on your requirements, you might instead want this:
moment.utc(vm.newHearing().HearingTime(), "HH:mm").toISOString()
Which is almost the same thing except that it assumes the input time is already in UTC.
Or you might want this:
moment(vm.newHearing().HearingTime(), "HH:mm").format("YYYY-MM-DDTHH:mm:ss")
This one doesn't try to convert to UTC at all.
For all choices, I emit the date string in ISO8601 format. Since you are sending it back to the server, this is the best choice. When you used toLocaleString, that generates a format that is appropriat for display only.