수업 014 (04.23 2020) - DateTime
2020. 4. 24. 00:51
728x90
1. App.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.IO;
namespace Study._015_2
{
class Item
{
}
class App
{
public App()
{
// 객체 생성
GameInfo gameInfo = new GameInfo();
// 불러오기
// 역직렬화 (문자열 > 객체)
gameInfo = JsonConvert.DeserializeObject<GameInfo>(json);
// 문자열 > Date Time
DateTime last = Convert.ToDateTime(gameInfo.strLastLoginTime);
DateTime now = DateTime.Now;
TimeSpan displacement = now - last; // var를 통해 형식 알아보기
Console.WriteLine(last);
Console.WriteLine(now);
Console.WriteLine(displacement);
Console.WriteLine(displacement.Minutes);
int disMin = displacement.Minutes;
if (disMin >= 10)
{
Console.WriteLine("하트 주기");
// gameIfo의 strLastLoginTime 의 값을 현재시간으로 바꾸고 저장!
gameInfo.strLastLoginTime = DateTime.Now.ToString();
// 객체 > 문자열
json = JsonConvert.SerializeObject(gameInfo);
// 파일저장
File.WriteAllText("./game_info.json",json);
}
else
{
// 현재 저장되어 있는 시간에서 10분뒤
// 문자열
DateTime rewardDateTime = last.AddMinutes(10);
Console.WriteLine("하트를 주려면 {0}분 {1}초 더 기달려주세요", remainTimespan.Minutes, remainTimespan.Seconds);
}
}
}
}
|
2. GameInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._015_2
{
class GameInfo
{
// 접속 시간
public string strLastLoginTime;
// 생성자
public GameInfo()
{
}
}
}
|
3. Result
728x90
'C# > Study' 카테고리의 다른 글
수업 015 (04.24 2020) 조건연산자(?:) F9,F5,F11 swap ref (0) | 2020.04.24 |
---|---|
수업 015 (04.24 2020) 복습 Inventory + percentage (0) | 2020.04.24 |
수업 014 (04.23 2020) 복습 - json, dictionary..etc (0) | 2020.04.23 |
수업 013 (04.22 2020) dictionary, json // Achievement (0) | 2020.04.22 |
수업 013 (04.22 2020) 복습 - dictionary<Tkey, TValue> (0) | 2020.04.22 |