如何在swift中的midnigs中获取UTC日期

时间:2017-01-13 12:12:21

标签: swift nsdate utc swift2.3

有人可以帮助我获取当前的UTC日期时间 我在尝试:

    var myLatLng = {lat: 36.5802466, lng: 127.95776367};
    document.getElementById('qkp-lat').value = myLatLng.lat;
    document.getElementById('qkp-lng').value = myLatLng.lng;
    window.postmap = new google.maps.Map(document.getElementById('postmap'), {
        center: myLatLng,
        zoom: 6,    
        mapTypeControl: false,
        streetViewControl: false,
        disableDefaultUI: true,
        mapTypeId: 'roadmap'
    });

    // GOOGLE MAP RESPONSIVENESS
    google.maps.event.addDomListener(window, "resize", function() {
     var center = postmap.getCenter();
     google.maps.event.trigger(postmap, "resize");
     postmap.setCenter(center); 
    });

    //MARKER
    window.PostAdMarker = new google.maps.Marker({
      map: postmap,
      position:myLatLng,
      draggable: true,
      anchorPoint: new google.maps.Point(0, -29)
    });
    //LOAD FROM CURRENT CITY
    var geocoder = new google.maps.Geocoder();
    //AFTER DRAG AND DROP SHOWS THE LAT AND LONG
    google.maps.event.addListener(PostAdMarker, 'dragend', function (event) {
        var latlng = {lat: this.getPosition().lat(), lng: this.getPosition().lng()};
        geocoder.geocode({'location': latlng}, function(results, status) {
          if (status === 'OK') {
            if (results[1]) {
                // saving to dom
                document.getElementById('qkp-lat').value = latlng.lat;
                document.getElementById('qkp-lng').value = latlng.lng;
            } else {
              window.alert('No results found');
            }
          } else {
            window.alert('Geocoder failed due to: ' + status);
          }
        });
    });

    var getlocDiv = document.createElement('div');
    var getlocvar = new getloc(getlocDiv, postmap);  
    getlocDiv.index = 1;
    postmap.controls[google.maps.ControlPosition.TOP_RIGHT].push(getlocDiv);



    // Create the search box and link it to the UI element.
    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
    postmap.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    // Bias the SearchBox results towards current map's viewport.
    postmap.addListener('bounds_changed', function() {
      searchBox.setBounds(postmap.getBounds());
    });

    var markers = [];
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      // Clear out the old markers.
      markers.forEach(function(marker) {
        marker.setMap(null);
      });
      markers = [];

      // For each place, get the icon, name and location.
      var bounds = new google.maps.LatLngBounds();
      places.forEach(function(place) {
        if (!place.geometry) {
          console.log("Returned place contains no geometry");
          return;
        }
        var icon = {
          url: place.icon,
          size: new google.maps.Size(71, 71),
          origin: new google.maps.Point(0, 0),
          anchor: new google.maps.Point(17, 34),
          scaledSize: new google.maps.Size(25, 25)
        };

        // Create a marker for each place.
        markers.push(new google.maps.Marker({
          map: postmap,
          icon: icon,
          title: place.name,
          position: place.geometry.location
        }));

        if (place.geometry.viewport) {
          // Only geocodes have viewport.
          bounds.union(place.geometry.viewport);
        } else {
          bounds.extend(place.geometry.location);
        }
      });
      postmap.fitBounds(bounds);
    });
    $$('body').on('touchstart','.pac-container', function(e){
        e.stopImmediatePropagation();
    })

但我得到let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let components = cal.components([.Day , .Month, .Year ], fromDate: NSDate()) let refDate = cal.dateFromComponents(components) 这似乎是UTC当地时间 我想在午夜获得当前日期的UTC。

在数据库中我将​​日期保存为秒数,但在服务器上它保存为1/13/2017 00:00:00并且在swift中我得到1/12/2017 23:00:00因此我无法获得值他们是不同的我不明白我需要做什么来获得相同的UTC。

在数据库中,我将日期保存为swift中的较长秒数{I} 1/12/2017 23:00:00,但日期不正确。

1 个答案:

答案 0 :(得分:0)

您需要将hour的{​​{1}},minutesecond属性设置为NSDateComponents

0

或者从iOS 8开始,您也可以使用let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! cal.timeZone = NSTimeZone(name: "UTC")! let components = cal.components([.Day , .Month, .Year,.Hour, .Minute, .Second], fromDate: NSDate()) components.hour = 0 components.minute = 0 components.second = 0 let refDate = cal.dateFromComponents(components)

startOfDayForDate