How to remove a specific field from specific document in MongoDB? - database

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?

Related

Timezone offset not working in angular js date expression

I have 2016-10-21T13:47:02.922452 as ISO string from backend server.
My timezone is +0530 GMT i.e Offset is +530 (ahead of GMT).
When i use angular date expression like this
{{'2016-10-21T13:47:02.922452'| date:'medium':'+530'}}
I expected output to be = Oct 21, 2016 7:17:02 PM
but it prints
Oct 21, 2016 1:47:02 PM instead.
I am confused for what am i doing wrong here.
Do something like this
var d = new Date('2016-10-21T13:47:02.922452');
console.log(d)
Answering myself !
The easiest way that i figured out ! Make a custom filter
app.filter('IST', function($filter){
return function(val){
var date = new Date(val);
return $filter(date, 'medium');
}
})
Then use filter in expression like this -
{{'2016-10-21T13:47:02.922452'| IST}}
Custom filter will automatically convert ISO format string to Date object (browser timezone will automatically take care of timezone conversion.)
Date pipe transforms the date string you are passing ('2016-10-21T13:47:02.922452') to a Date object and then applies the time zone offset.
¿Whats the problem? When transforming '2016-10-21T13:47:02.922452' to a Date, it supposes that the date is in your local time, so the final calculation is wrong. For example, I am in +0200 so the transformation will be:
2016-10-21T13:47:02.922452 -> 2016-10-21T13:47:02.922452+0200 (Date object)
2016-10-21T13:47:02.922452+0200 -> 2016-10-21T18:17:02.922452+0530 (date changes with offset difference 0530-0200)
If your backend date is always in GMT timezone, just add Z to your date string: 2016-10-21T13:47:02.922452Z. This way, when creating the Date it will understand that the initial timezone is +0000
Solution:
{{'2016-10-21T13:47:02.922452Z' | date:'medium':'+0530'}}

moment to date object timezone issue

I have a moment.js object generated from fullcalendar in BST that looks like this:
console.log(momentSelected)
//Moment {_isAMomentObject: true, _isUTC: true, _offset: 0, _locale: f, _d: Tue May 03 2016 01:00:00 GMT+0100 (BST)…}
I don't want a BST time but a UTC time that looks like this:
console.log(momentSelected.format('YYYY-MM-DD HH:mm Z'));
//2016-05-03 00:00 +00:00
Now I need to convert it into a Date object:
$scope.date = new Date(momentSelected.format('YYYY-MM-DD HH:mm Z'));
console.log($scope.date);
//Wed May 04 2016 01:00:00 GMT+0100 (BST)
The last output is wrong... I want Wed May 04 2016 00:00:00+00:00 (UTC)
You can create a Date object using the toDate function on the moment object.
$scope.date = momentSelected.toDate();
However, you must recognize that the nature of the Date object is that it will always represent UTC internally, and its toString function will always reflect the local time zone where the code is running.
If your local time zone is UK (alternating between GMT and BST for daylight saving time), then it is impossible to get (UTC) time in the string produced by console.log($scope.date);, regardless of how you created that date.
This is why it is better to use moment's format function and display that string directly. A moment can reflect UTC, local time, and other time zones. A Date object cannot.
Also, you should pay no attention to the underscore-prefixed internal fields of a moment object. Use the public API instead. See the moment user guide.

How can I convert the date column to a different builtin date type in excel (Thu Aug 04 00:00:00 BST 1983)

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)

How to save value from a Date Input Field in Laravel?

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?

Convert String to datetime in SQL Server 2008

How will I convert date in the format Sat Mar 29 00:00:00 EST 1975 to datetime in TSQL?
I get the date in this format from an old table which defined the date of birth column as NVARCHAR and stores the data as Mon Jun dd hh:mm:ss GMT yyyy format. I need to read another table which has the dob in datetime using this value.
So basically I want to convert, say Sat Mar 29 00:00:00 EST 1975 to 1975-03-29 00:00:00.000
Is there a way in T-SQL to do this conversion? I tried the CONVERT function, but I am unable to locate the correct 'style' to use.
Examining the data format, it appears to be a fixed length string.
The first portion is the day of week, which can be discarded as it isn't needed for parsing. Next you have the month and day information, which we need. After that is the time, which can be retained or discarded depending on whether you want a date or datetime as output.
Since you are looking for a date of birth, the time zone information can most likely be safely discarded.
Finally, there is the year.
If we eliminate the day of week and the time zone, sql server will parse the rest of the string with no problem.
I recommend cast(substring(#difficultTime,5,7) + substring(#difficultTime,25,4) as date), where #difficulteTime is the column name you are converting.
If you wanted to retain the time information, the following format will work cast(substring(#difficultTime,5,16) + substring(#difficultTime,25,4) as datetime)
This assumes that your strings will be of a fixed length. The first conversion shown eliminates the day of week, the time, and the time zone from the string, leaving a parseable date.
The second conversion eliminates the day of week and the time zone, leaving a parseable datetime.

Resources